-
b) and c) have the same result, a) has error
foldr (*) 1 [1..5] -- b) foldl (flip (*)) 1 [1..5] -- c) foldl (*) 1 [1..5]
-
evaluation steps of
foldl (flip (*)) 1 [1..3]
foldl (flip (*)) 1 [1..3] = foldl (flip (*)) (1 * 1) [2,3] = foldl (flip (*)) (2 * (1 * 1)) [3] = foldl (flip (*)) (3 * (2 * (1 * 1))) [] = (3 * (2 * (1 * 1)))
-
c) one difference between
foldr
andfoldl
is thatfoldr
, but notfoldl
, associates to the right -
a) folds are catamorphisms, they are used to reduce structure
-
fixed functions
-
foldr (++) "" ["woot", "WOOT", "woot"]
-
foldr max "" ["fear","is","the","little","death"]
-
foldr (&&) True [False, True]
-
foldr (||) True [False, True]
always returnTrue
-
foldl (\x y → x ++ show y) "" [1..5]
-
foldr (flip const) 'a' [1..5]
-
foldr (flip const) 0 "tacos"
-
foldl const 0 "burritos"
-
foldl const 'z' [1..5]
-
The fibs
using scanl
is a little bit of a mind bender at the beginning. I wrote out the evaluation https://gist.github.com/lukleh/67cf0a78205d3f6bd2d9
link:ch10_10.9_0.hs[role=include]
-
tuples
link:ch10_10.10_0.hs[role=include]
-
following function return average size (round to integer) of word in a sentece (string)
link:ch10_10.10_1.hs[role=include]
-
returing
Fractional
valueslink:ch10_10.10_2.hs[role=include]
-
myOr
myOr :: [Bool] -> Bool myOr = foldr (||) False
-
myAny
link:ch10_10.10_3.hs[role=include]
-
myElem
link:ch10_10.10_4.hs[role=include]
-
myReverse
link:ch10_10.10_5.hs[role=include]
-
myMap
link:ch10_10.10_6.hs[role=include]
-
myFilter
link:ch10_10.10_7.hs[role=include]
-
squish
link:ch10_10.10_8.hs[role=include]
-
squishMap
link:ch10_10.10_9.hs[role=include]
-
squishAgain using squishMap
link:ch10_10.10_10.hs[role=include]
-
myMaximumBy
link:ch10_10.10_11.hs[role=include]
-
myMinimumBy
link:ch10_10.10_12.hs[role=include]