[Haskell-beginners] Please help me to understand: ($ 3)

Costello, Roger L. costello at mitre.org
Sat May 4 19:33:49 CEST 2013


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




More information about the Beginners mailing list