Real World Haskell 4.関数プログラミング 4.6 ループをどのように考えるか
-- file: ch04/Fold.hs myFoldl :: (a -> b -> a) -> a -> [b] -> a myFoldl f z xs = foldr step id xs z where step x g a = g (f a x)
import Debug.Trace myFoldl f z xs = trace("foldr step id "++show xs++" "++show z) (foldr step id xs z) where step x g a = trace("g (f "++show a++" "++show x++")") (g (f a x))
myFoldl (\x y->"["++x++" "++y++"]") "[start]" ["a","b","c"] "foldr step id ["a","b","c"] "[start]" g (f "[start]" "a") -- f は(\x y->"["++x++" "++y++"]") g (f "[[start] a]" "b") -- g は id g (f "[[[start] a] b]" "c") [[[[start] a] b] c]"