Please help me to understand: ($ 3)

Hi Folks, The type signature of ($) is: ($) :: (a -> b) -> a -> b That tells me that ($) is a function that takes two arguments: 1. A function which maps values of type "a" to values of type "b" 2. A value of type "a" and it returns a value of type "b". Okay, I can understand that. So, I created some examples: ($) odd 3 -- returns True odd $ 3 -- returns True Good. 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? 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? /Roger

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

On Sat, May 4, 2013 at 1:33 PM, Costello, Roger L.
($) odd 3 -- returns True odd $ 3 -- returns True
Good.
But then I saw this in an article:
($ 3) odd
What does ($ 3) mean? I thought the first argument to ($) is a function?
If you have an infix operator, you can turn it into a function by wrapping it in parens, as you know. But you can also go further: you can include one of the parameters in the parens, producing a partially applied function; Haskell calls this a section. ($ 3) is a section which has partially applied the right-hand parameter to ($), resulting in a function which expects the left-hand parameter (the function). If you were writing this out "longhand", it would be \x -> x $ 3 Similarly, (+ 3) is a section on (+) (and (3 +) is the opposite section; since (+) is commutative, it is indistinguishable from (+3) in practice. ($) is not commutative, since one parameter is a function and the other is a value to apply the function to, so (x $) and ($ x) are different operations.
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?
map is not infix, so that means something different; you have simply applied the first parameter, but that wants to be a function, not a list. If you rephrase it as infix: (`map` list) odd -- `foo` is the function foo as an infix, that is, (x `foo` y) is (foo x y). note ` not ' ! it will work. -- brandon s allbery kf8nh sine nomine associates allbery.b@gmail.com ballbery@sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net
participants (3)
-
Brandon Allbery
-
Costello, Roger L.
-
Denis Kasak