Ch 05 Daily 2
Stone age functions: write using a minimum of Haskell tools. Concentrate on writing well-crafted functions.
Good to use
-
Pattern matching. Example:
(x:xs)
. -
Recursion.
-
“Multiple definitions.” Example:
f 0 = 0 f 1 = 1 f n = f (n-1) + f (n-2)
-
Basic functions like arithmetic,
:
and++
.
Avoid
- Conditionals (guards) and if statements. Use “multiple definitions” whenever possible.
- Using
head
andtail
. Choose pattern matching instead. - Counting the items in a list (
length
and similar constructions). Look for another way to achive what you want, maybe with a recursive function.
Group I
You can just use a type of Int
if you prefer not to think about the
generic type parameter a
.
mylength :: [a] -> [Int]
mylast :: [a] -> a
mynull :: [a] -> Bool
. Should be be fast for an input[1..1000*1000*1000]
.mysum :: [Int] -> Int
myzip :: [a] -> [b] -> [(a,b)]
Group II
You can use a type of Int
instead of a
if you prefer.
myreplicate :: Int -> a -> [a]
myminimum :: [a] -> a
Requires eithermin
or using guards.mytake :: Int -> [a] -> [a]
myreverse :: [a] -> [a]
Group III (Skip)
- Very light work:
mychoice :: a -> a -> Bool -> a
. This one gives the first input when the boolean value is true, otherwise it gives the second input.
Group IV: Combinations
there is one function to write: produce all of the k element combinations of the elements in a given list. This function can be challenging. We intend to work through creating an example editing the code.
- Write a function
allComb :: Int -> [a] -> [[a]]
to create a list of all “combinations” of a given number of elements of the list. Combinations means the order you write the elements in does not matter, so you should only give one of[1,2]
or[2,1]
.
Notes:
- Write examples and look for patterns.
- Using
reverse
is misleading; it really only appears for 2 elements. Starting with 3 elements the pattern is more complex. - If factorial appears in your answer, look for a simpler, more direct approach.
- Ignore the possibility that some of the elements are the same (“not distinct”).