
On Tuesday 06 July 2010 13:59:08, Tom Hobbs wrote:
Right, a bit of playing around and I think understand. Maybe.
In your example;
map ($ 3) functionList
I'm assuming that this is Haskel's way of saying "Give the value 3 as an argument to each function in functionList".
Right. ($ 3) is a right-section of ($), and like (^3) n ~> n^3, ($ 3) f ~> f $ 3 -- (= f 3).
Playing in Hugs seems to suggest that this is the case;
Hugs> map ($ 3) [(4 +), (5 +), (6 +)] [7,8,9]
That makes sense. The first arg to map is expecting a function, so we give it a function and a value which just returns the value. I can see why that works.
Rewriting using "flip id" has me stumped though.
Hugs> map (flip id 3) [(4 +), (5 +), (6 +)] [7,8,9]
So this is saying,
Actually, I don't know. Working it out in my head I would say "id 3" returns 3. But "flip 3" causes an error in Hugs, so this code doesn't work - but clearly it does.
Wrong parentheses. flip id 3 = (flip id) 3 so (flip id) 3 f ~> (id f) 3 ~> f 3 See, flip :: (a -> b -> c) -> b -> a -> c id :: t -> t to make flip id typecheck, only a subset of all possible types of id can be used here, namely, t must be a function type, t ~ (u -> v) then id :: (u -> v) -> u -> v ---------a--------b----c (remember that (->) associates to the right, so (u -> v) -> (u -> v) is the same as (u -> v) -> u -> v) and flip id :: u -> (u -> v) -> v then flip id 3 :: Num n => (n -> v) -> v
Hugs> :t flip id 3 flip id 3 :: Num a => (a -> b) -> b
You should have tried without the 3, Prelude> :t flip id flip id :: b -> (b -> c) -> c I did mention that it's confusing for beginners, as you saw, it really is. But I hope your thinking about it makes the explanation more effectful than if I had given it right away.
Hugs> :t ($ 3) flip ($) 3 :: Num a => (a -> b) -> b
Is presumably why it works, but I can't work out how to create the type signature of "flip id 3" from the sigs of flip and id.
Help?
Thanks.
Tom
Cheers, Daniel