Third Week FMFP

The "Listtatzelwurm" or "Listmonster" you might already know from the tutorial. It's from the site "learn you a haskell for greater good", if you need more help about list, the chapter with the Listmonster might be a help for you.
Let me give you some extra exercises for the current chapter. The solution will be online in around a week.
Usage of List
Multiply all numbers from 1 to 100 by using a prelude function (and no recursion)
Sum all odd numbers bellow 50. Use a prelude function and the haskell list in a clever way.
Generate the string "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz" (remember a string in haskell is just a ... of ...)
List functions
TA's usually just let you implement prelude functions to train list functions. So then, implement this functions recursiv:
sum :: (Num a) => [a] -> a merge :: [a] -> [a] -> [a] -- merge two list (in haskell (++)) nthElement :: [a] -> Int -> a -- give the n-th element of a list (in haskell (!!))
List comprehension
You already talked about list comprehension in the lecture. If you want to repeat it a bit, read the first chapter here http://www.haskell.org/haskellwiki/List_comprehension. Then find all numbers bellow 1000 that are devidable by 3 or 5 with a list comprehension. (When you sum this up you have already solved the first problem of project euler.)
A Proof for the end
You can also do recursiv proofs over lists. Given the following programm:
map f [] = [] (m.1) map f (x:xs) = f x : map f xs (m.2) times2 x = 2*x (ti) twice [] = [] (tw.1) twice (x:xs) = (2* x):(twice xs) (tw.2)
Prove "FORALL xs::[int]. twice xs = map times2 xs" with recursion.
comments
add a comment
The Trackback URL to this comment is:
http://leo.freeflux.net/blog/plugin=trackback(2820).xml





