3 ms·
In Haskell, all functions take one argument. Also, operators are functions. First, the one argument thing is known as currying. With higher-order functions, re
by michaelochurch 12y ago
In Haskell, all functions take one argument. Also, operators are functions.
First, the one argument thing is known as currying. With higher-order functions, returning a function is admissible, so
map :: (a -> b) -> [a] -> [b]
can also be thought of as:
map :: (a -> b) -> ([a] -> [b])
like so:
ghci> :t map length
map length :: [[a]] -> [Int]
As for operators being functions, function names that are alphanumeric default to prefix (e.g. f 10) while those that are symbolic default to infix (e.g. 10 + 18). To use an alphanumeric name in infix, use backticks, e.g.:
ghci> 5 `max` 6
6
Use parentheses to prefix an "operator"-style name, like so:
ghci> (+) 5 6
11
The (+1) that you're meeting is an additional bit of syntactic sugar. It's identical to (\x -> x + 1). It allows you to do things like this:
ghci> map (2^) [1..5]
[2,4,8,16,32]
ghci> map (^2) [1..5]
[1,4,9,16,25]