I started to learn Haskell. I'm curious about why in Haskell the function is taken as the first argument for the higher order functions for lists. For example here is a definition of map
:
map :: (a -> b) -> [a] -> [b]
It means that I can use it either in prefix or operator form like that:
-- yields [3,5,7]
map (+ 2) [1,3,5]
-- we can chain like that
filter (< 4) (map (+ 2) [1,3,5])
-- or in operator form
(+ 2) `map` [1,3,5]
(< 4) `filter` ((+ 2) `map` [1,3,5])
In Scala the same can be written as follows:
List(1,3,5) map (_ + 2)
// we can chain as follows:
List(1,3,5) map (_ + 2) filter (_ < 4)
So the order is reversed and we take the function as the second argument. What is the reason on arguments ordering in Haskell?
squareList = map (**2)
. You can "upgrade" a function to work on lists. What's not convenient about that? – Mateen Ulhaqmap
is alsofmap
.fmap
is usually thought of as something that "upgrades" functions to work with special types (Functors). – Mateen Ulhaqmap
- it's the only argument tomap
. The function couldn't be on the left of the method name because then you'd be callingmap
on the function object, not on the list, and functions don't have a method namedmap
. – sepp2k