Let's clear up some misconceptions that beginners might make.
You may have encountered functions such as:
max :: (Ord a) => a -> a -> a
max m n
| m >= n = m
| otherwise = n
Beginners will typically view max :: (Ord a) => a -> a -> a
as function that takes two arguments (values) of type a
and returns a value of type a
. However, what is really happening, is that max
is taking one argument of type a
and returning a function of type a -> a
. This function then takes an argument of type a
and returns a final value of type a
.
Indeed, max
can be written as max :: (Ord a) => a -> (a -> a)
Consider the type signature of max
:
Prelude> :t max
max :: Ord a => a -> a -> a
Prelude> :t (max 75)
(max 75) :: (Num a, Ord a) => a -> a
Prelude> :t (max "Fury Road")
(max "Fury Road") :: [Char] -> [Char]
Prelude> :t (max "Fury Road" "Furiosa")
(max "Fury Road" "Furiosa") :: [Char]
max 75
and max "Fury Road"
may not look like functions, but in actuality, they are.
The confusion stems from the fact that in mathematics and many, other, common programming languages, we are allowed to have functions that take multiple arguments. However, in Haskell, functions can only take one argument and they can return either values such as a
, or functions such as a -> a
.