Write the following functions, which all belong to Haskell's standard library:
a) iterate :: (a → a) → a → [a]
iterate f x = [x, f x, f (f x), …]
b) takeWhile :: (a → Bool) → [a] → [a]
find :: (a → Bool) → [a] → Maybe
apartition :: (a → Bool) → [a] → ([a], [a])
span :: (a → Bool) → [a] → ([a], [a])
Write a function altMap
that takes
two functions and a list. altMap
should apply the two
functions alternately to list elements. For example:
altMap (\i ->
i + 1) (\i -> i - 1) [1..8] == [2, 1, 4, 3, 6, 5, 8, 7]
a) Write the following function (which is in Haskell's standard library in the Data.List module):
> maximumBy (\s t -> compare (length s) (length t)) ["one", "fine", "day"] "fine"
b) Write a function maxBy :: Ord b => (a -> b) -> [a]
-> a
that takes a function f and a list of values, and
returns the value x for which f(x) is largest.
For example:
> maxBy length ["one", "fine", "day"] "fine"
Recall the description of the built-in functions foldl and foldr:
What are the values of these expressions?
foldl (-)
0
[
1
,
2
,
3
]
foldr (-)
0
[
1
,
2
,
3
]
Write the functions foldl and foldr.
Write the folowing functions using foldl or foldr:
map :: (a → b) → [a] → [b]
filter :: (a → Bool) → [a] → [a]
elem :: Eq a => a → [a] → Bool
reverse :: [a] → [a]
Write a function commonWords :: Int ->
String -> String
that takes an integer N and a string text,
and returns a string describing the N most frequent words in the
text. Each line in the output string should contain a word and its
count, separated by a space. Convert all words to lowercase.
For example, the output string might look like this:
the 23 a 18 for 15 we 10 horse 6 mill 3