-
Exercise 9.12
Instances where we can curry and convert anonymous functions to sections are easy to find, for example from Exercise 5.5: doubleEach” :: [Int] -> [Int] doubleEach” = map (*2)
-
Exercise 9.11
map f (map g xs) = map (f.g) xs map (\x -> (x+1)/2) xs = map (/2) (map (+1) xs)
-
Exercise 9.10
map ((/2).(+1)) xs
-
Exercise 9.9
fix f = f (fix f) First, f takes the result of fix f, and second, the return type of f must be the same as the return type of fix. fix :: (a -> a) -> a Research shows that fix is the Fixed point combinator (or Y-combinator). Apparently this allows the definition of […]
-
Exercise 9.8
power :: (a -> a) -> Int -> a -> a power f 0 = (\x -> x) power f n = (\x -> f (power f (n-1) x)) Since the function is called power, there is one obvious application that springs to mind, i.e. raising to a power: raise :: Int -> Int -> […]
-
Exercise 9.7
twice :: (a -> a) -> a -> a twice f = (\x -> f (f x)) twice twice applies the function 4 times: twice twice f = twice (\y -> f (f y)) = (\x -> (\y -> f (f y)) ((\y -> f (f y)) x)) = (\x -> (\y -> f (f […]
-
Exercise 9.6
appendr = foldr (flip (++)) [] appendr [x,y,z] = foldr (flip (++)) [] [x,y,z] = flip (++) x (flip (++) y (flip (++) z [])) = flip (++) x (flip (++) y ([] ++ z)) = flip (++) x (([] ++ z) ++ y) = (([] ++ z) ++ y) ++ x Running time of […]
-
Exercise 9.5
applyAll :: [(a -> a)] -> a -> a applyAll [] x = x applyAll (f:fs) x = f (applyAll fs x)
-
Exercise 9.4
applyEach :: [(a -> b)] -> a -> [b] applyEach [] _ = [] applyEach (f:fs) x = f x : applyEach fs x
-
Exercise 9.3
ys :: [Float -> Float]