{- We LOVE lists! (part 1) Lists are probably the most commonly used datatype of any language .. For this reason in Haskell there is a special builtin syntax for lists. [] is the empty list x:xs is the list with first element x and "tail" the list xs [1,2,3,4,5,6] an example of a list of integers [a] is (besides the one elemnet list) the type of list of a's -} -- appending lists myappend:: [a] -> [a] -> [a] myappend [] ys = ys myappend (x:xs) ys = x:(myappend xs ys) -- in the prelude append is defined as an operator -- (++) :: [a] -> [a] -> [a] -- [] ++ ys = ys -- (x:xs) ++ ys = x:(xs ++ ys) myflatten::[[a]] -> [a] myflatten [] = [] myflatten (xs:xss) = myappend xs (myflatten xss) -- in the prelude this is concat BUT look up its type!! mylength:: [a] -> Integer mylength [] = 0 mylength (x:xs) = 1 + (mylength xs) -- in the prelude this is length mymember:: Eq a => a -> [a] -> Bool mymember a [] = False mymember a (x:xs) | a==x = True | otherwise = mymember a xs --mapping a function over a list mymap:: (a -> b) -> ([a] -> [b]) mymap f [] = [] mymap f (a:as) = (f a):(mymap f as) --given a list of booleans compute their conjunction myAND:: [Bool] -> Bool myAND [] = True myAND (b:bs) = b && (myAND bs) --given a list of booleans compute their disjunction myOR:: [Bool] -> Bool myOR [] = False myOR (b:bs) = b || (myOR bs) -- Checking whether there is something in the list which satisfies -- a predicate inlist:: (a -> Bool) -> [a] -> Bool inlist pred as = myOR (mymap pred as)