Functions
Function Definition and Application
Hamler:
add x y = x + y
add 3 4 -- return 7
factorial 0 = 1
factorial n = n * factorial (n - 1)
factorial 10 -- return 3628800
Erlang:
add(X, Y) -> X + Y.
add(3, 4). %% return 7
factorial(0) -> 1;
factorial(N) -> N * factorial(N - 1).
factorial(10). %% return 3628800
Anonymous Functions
Hamler:
add = \a b -> a + b
curried_add = \a -> \b -> a + b
add 2 3
curried_add 2 3
Erlang:
Add = fun(X, Y) -> X + Y end.
CurriedAdd = fun(X) -> fun(Y) -> X + Y end end.
Add(2,3).
(CurriedAdd(2))(3).
Guards in Function
Hamler:
f :: Integer -> String
f n | n > 0 = "Positive Integer"
| n < 0 = "Negative Integer"
| otherwise = "Zero"
Erlang:
f(N) when N > 0 -> "Positive Integer";
f(N) when N < 0 -> "Negative Integer";
f(_) -> "Zero".
当前内容版权归 hamler-lang 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 hamler-lang .