flatten
function in Haskell, like the one we
wrote for Python. Try to use
the declarative style.
Now write flatten
using foldl
.
Can we write superflatten
in Haskell? (Hint: what would
be its type?)
[ f x | x <- xs, p x ]In English: "compute the list of (f x) for all elements x in xs for which (p x) is true". For example:
[ x * 2 | x <- [1..4], x /= 3] -- prints [2, 4, 8]
Why do you think Haskell made this a built-in construct? Can you "desugar" it into other, primitive forms? What if xs is an infinite list? (Remember Haskell is a lazy language.)
Use the built-in functions ord
, map
, and
foldl
to define a function hash
which hashes
a string by summing the ASCII values of its characters. Try it out on
"MSDOS 6.000" and "SYSTEM 7.0". (This problem inspired by Matteo
Frigo.)
ord :: Char -> Int -- returns (f x1) : (f x2) : ... : (f xn) : [] map :: (a->b) -> [a] -> [b] map f [] = [] map f (x:xs) = f x : map f xs -- returns (((z f x1) f x2) ... f xn) foldl :: (a->b->a) -> a -> [b] -> a foldl f z [] = z foldl f z (x:xs) = foldl f (f z x) xs