July 2023
Intermediate to advanced
670 pages
17h 13m
English
Functions in Haskell have types just like non-function values do. The types of Haskell functions are written using an arrow (->). Let’s look at an example of a function that takes an Int and adds 1 to it:
| | addOne :: Int -> Int |
| | addOne n = n + 1 |
The type signature for this function says that it is going to accept an Int and will return an Int. From the type signature, you can also know that the variable n must be an Int. That’s because n is the first parameter to the function, and we’ve said in the function’s type that its parameter must be an Int.
It’s also possible to have function types that have parameters even when you’re not explicitly binding the parameter to a variable. For example, we can rewrite ...