foldl f z [] = z
foldr f z [] = z
map f [] = []
Recursive definitions:
foldl f z (x:xs) = foldl f (f z x) xs
foldr f z (x:xs) = f x (foldr f z xs)
map f (x:xs) = f x : map xs
Write the following functions.
fold()
or map()
; instead, it
should be implemented with a combination of fold()
and
map()
.
concat [ [a,b,c], [d,e,f],
[g], [h,i] ] -> [a,b,c,d,e,f,g,h,i]
(key, [v1, v2,...])
. There are two possible
implementations; one uses a hash table and one does not. This
function should not be passed to fold()
or map()
; instead, it should be implemented with a
combination of fold()
and map()
.
group [ [k1,a], [k2,b], [k1,c], [k1,d], [k3,e],
[k2,f] ] -> [ [k1,[a,c,d]], [k2,[b,f]], [k3,[e]] ]
f
that, given a list of integers and
a value k
, can be folded over the list such that the
result of the fold is two lists partitioned around the value
k
. Feel free to hardcode the value k
in function f
.
k = 3
. Then foldl f z
[8,2,6,1,3,6,2,0] -> ([2,1,2,0], [3,8,6,6])
Normally, hardcoding values is bad. However, since this is
Haskell, I don't care that you hardcode k
into
f
as it's trivially fixable. Why is that? What two
language features makes this fix easy?
f
and g
, the "." operator
in Haskell will create a third function that is the composition of
the two. Write the code to do this (should be really short).