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
aspan :: (a → Bool) → [a] → ([a], [a])
elemIndex :: Eq a => a → [a] → Maybe Int
maybe :: b -> (a -> b) -> Maybe a -> b
group :: Eq a => [a] → [[a]]
group "Mississippi" ==
["M","i","ss","i","ss","i","pp","i"]
A curried function takes its arguments one at a time:
> sum 3 4 7
An uncurried function takes its arguments in a tuple:
> sum (3, 4) 7
Write a function curry
that converts an uncurried
function of two arguments to a curried function, and a function
uncurry
that performs the inverse transformation.
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]
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
Write a function triples
that takes
an integer N and returns a list of integer triples (a, b, c) with
0 < a < b < c ≤ N such that
a2 + b2 = c2. If two triples are
multiples of each other (e.g. (3, 4, 5) and (6, 8, 10)),
only the smaller of them (e.g. (3, 4, 5)) should appear in the list.
> triples 15 [(3,4,5),(5,12,13)]