
On 4 May 2013 19:33, Costello, Roger L.
But then I saw this in an article:
($ 3) odd
What does ($ 3) mean? I thought the first argument to ($) is a function?
I checked the type of ($ 3) and it is:
($ 3) :: Num a => (a -> b) -> b
I don't understand that. How did that happen? Why can I take a second argument and wrap it in parentheses with ($) and then that second argument pops out and becomes the argument to a function?
These are called operator sections. Take a look at http://www.haskell.org/haskellwiki/Section_of_an_infix_operator
I decided to see if other functions behaved similarly. Here is the type signature for the "map" function:
map :: (a -> b) -> [a] -> [b]
That looks very similar to the type signature for ($). So, I reasoned, I should be able to do the same kind of thing:
let list=[1,2,3] (map list) odd
But that fails. Why? Why does that fail whereas a very similar looking form succeeds when ($) is used?
Because (map list) is an ordinary function application since operator sections apply only to infix operators. On the other hand, had you written (`map` list), it would have worked as you expected. λ. let list = [1, 2, 3] λ. (`map` list) odd [True,False,True] -- Denis Kasak