Functions
When we define a new function, we can give it a type signature. For example double
is a function takes an Integer
and gives an Integer
doubled as output.
double :: Integer -> Integer
double x = x * 2
Lambda Expression
There are also lambda expressions in Hamler, here is an example of how we rewrite double.
double' :: Integer -> Integer
double' = \x -> 2 * x
It becomes really handy when we need to make an anonymous function.
Currying
--Curry
--This is uncurried (+)
add :: (Integer, Integer) -> Integer
add (x, y) = x + y
--This is curried (+)
plus :: Integer -> Integer -> Integer
plus x y = x + y
Partial Application
-- plus :: Integer -> (Integer -> Integer) This is one of the example of higher order functions
>:t plus 2
plus 2:: Integer -> Integer
>let plusTwo = plus2
>plusTwo 3
5
当前内容版权归 hamler-lang 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 hamler-lang .