Reference Information. I study anonymous recursion, and I take on the task of implementing foreplay without using any named recursion to help it sit well in my mind. I'm not quite there yet, and along the way I came across something unrelated, but still interesting.
map1 = \f -> \x -> if (tail x) == [] then [f (head x)] else f (head x) : (map1 f (tail x)) map2 fx = if (tail x) == [] then [f (head x)] else f (head x) : (map2 f (tail x)) map3 f (x:xs) = if xs == [] then [fx] else fx : (map3 f xs) map4 f (x:[]) = [fx] map4 f (x:xs) = fx : map4 f xs
The GHC complains about the first, excellent with the second, and the third and fourth - just to show how they can be implemented in different ways.
*Main> map1 (*2) [1..10] <interactive>:1:15: No instance for (Num ()) arising from the literal `10' Possible fix: add an instance declaration for (Num ()) In the expression: 10 In the second argument of `map1', namely `[1 .. 10]' In the expression: map1 (* 2) [1 .. 10] *Main> map2 (*2) [1..10] [2,4,6,8,10,12,14,16,18,20] *Main> map3 (*2) [1..10] [2,4,6,8,10,12,14,16,18,20] *Main> map4 (*2) [1..10] [2,4,6,8,10,12,14,16,18,20]
If I add a type signature to map1, all this is good.
map1 :: Eq a => (a -> b) -> [a] -> [b]
The first two functions seem almost the same to me, so I suppose my question is simply "What is going on here?"