FYI: Second day into Haskell
MRE (what the function does, does not matter):
foo :: Int -> Int -> Int
foo x y
| x == y = x + y
| x < y = x
| x > y = y
This is what I've gathered about the ->
operator(?) so far:
- It is right-associative
- It is used in function type declaration to denote the type of function parameters as well as its return type
Now take a look at the following function type declaration:
foo :: Int -> Int
This declares(what is the appropriate word?) a function called foo
that takes an Int
and returns an Int
.
However, the declaration: foo :: Int -> Int -> Int
declares(?) that foo
takes 2 Int
s and returns an Int
when it is the same as saying foo :: Int -> (Int -> Int)
which is a function that takes an Int
and returns a function that takes an Int
and returns an Int
.
How does this make sense? Should it not be something on the lines of Int,Int -> Int
?
PS: The book I'm using: Thinking Functionally with Haskell by Richard Bird.
foo:: Int -> Int -> Int
is "a function that takes anInt
and returns a function that takes anInt
and returns anInt
", but when function application is written asfoo a b
the differentiation is almost meaningless, it's the same as(foo a) b
foo
is a function that produces another function,foo a
is that function applied toa
, sincefoo
s return value is a function we can appy that to another value:foo a b
is the returned functionfoo a
applied tob
.