sq :: Integer -> Integer
sq n = n * n
::
can be read as ‘has the type’. Here sq
is the function name which takes an integer and returns an integer.
mul :: Integer -> Integer -> Integer
mul a b = a * b
the first line is type signature, while this is useful, it is not required, as haskell compiler can infer the type automatically. Second line is the actual function definition, =
is used to define the function
Haskell functions always take only one parameter, In this case it is to say the function mul
takes one parameter and returns another function which take a Interger and return an Integer, The ->
is right associative, meaning the above function can be rewritten as below altough brackets are not necessary.
mul :: Integer -> (Integer -> Integer)
The functions mul and sq can be written a lambda functions,
sq = \n -> n * n
mul = \a b -> a * b
functions can be called as prefix notation or infix (not all functions can be represented as infix)
prefix example
mul 32 45
infix example, backticks should be used when functions is used as infix
32 `mul` 45
The infix functions like +, -, *, /
etc, if used as prefix, they should be surrounded with brackets
(+) 3 4
Site design and logo and content © fossix.org