Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /home/zhenxiangba/zhenxiangba.com/public_html/phproxy-improved-master/index.php on line 456
{- Google's MapReduce programming model revisited The following functions are readily defined in the Haskell Prelude. -} module Fold where import qualified Prelude (words) import Prelude hiding (words,foldl,foldr,map) import Data.Char -- Definition of map in terms of pattern matching on lists map :: (a -> b) -> [a] -> [b] map f [] = [] map f (x:xs) = f x : map f xs -- Definition of foldl in terms of pattern matching on lists foldl :: (b -> a -> b) -> b -> [a] -> b foldl f b [] = b foldl f b (a:as) = foldl f (f b a) as -- Definition of reduce by plain type specialization reduce :: (a -> a -> a) -> a -> [a] -> a reduce = foldl -- Definition of foldr in terms of pattern matching on lists foldr :: (a -> b -> b) -> b -> [a] -> b foldr f b [] = b foldr f b (a:as) = f a (foldr f b as) -- Definition of foldl in terms of foldr foldl' :: (b -> a -> b) -> b -> [a] -> b foldl' f = flip (foldr (\a f' -> (\b' -> f' (f b' a))) id) -- Definition of map in terms of foldr map' :: (a -> b) -> [a] -> [b] map' f = foldr (\a b -> f a : b) [] -- -- Split a string into words -- We simply look for "spaces" as separators. -- (There is of course such a function in the Prelude.) -- words :: String -> [String] words = reverse . snd . foldl transition (False,[]) where transition (state, words) char = if state then if isSpace char then (not state, words) else (state, (head words ++ [char]) : tail words) else if isSpace char then (state, words) else (not state, [char]:words) -- Test harness main = do print $ words myPhrase == Prelude.words myPhrase where myPhrase = "The under-appreciated unfold"