-- Some examples of using anonymous functions, currying, and composition -- by Scot Drysdale on 10/11/07 -- flip :: (a -> b -> c) -> b -> a -> c -- flip f x y = f y x --from Standard Prelude: myreverse = foldl (flip (:)) [] pairwiseAverageLists = zipWith (\ x y -> (x + y)/2) notSame :: (Eq a) => [(a,a)] -> [(a,a)] notSame = filter (\(x,y) -> x /= y) reversePairs = map (\(x,y) -> (y,x)) -- Functional composition + currying is powerful -- Version we wrote for SA 1 distance :: [Double] -> [Double] -> Double distance p1 p2 = sqrt (foldl (+) 0.0 (map (^2) (zipWith (-) p1 p2))) -- Uses functional composition, except for the last. Can't use it because -- zipWith is 2-parameter function. distanceC :: [Double] -> [Double] -> Double distanceC p1 p2 = sqrt . foldl (+) 0.0 . map (^2) $ zipWith (-) p1 p2 -- Uses functional application operator $ to apply each function -- to what follows distanceA :: [Double] -> [Double] -> Double distanceA p1 p2 = sqrt $ foldl (+) 0.0 $ map (^2) $ zipWith (-) p1 p2