data Edge a = E a a -- Edge type required for Problem #1 -- Problem 1 self_loop [] = False self_loop (E n m : edges) = n == m || self_loop edges -- Problem 2 index_of _ [] = Nothing index_of item (x:xs) | item == x = Just 0 | otherwise = increment (index_of item xs) increment (Just n) = Just (n+1) increment Nothing = Nothing -- Problem 3 (two versions) impervious fn inputs = filter (\item->item == fn item) inputs impervious2 fn inputs = [x | x<-inputs, x == fn x] -- Problem 4 (three versions) sum_all_starting_with item lists = sum (map sum (filter (\lst->head lst == item) lists)) sum_all_starting_with2 item lists = sum (concat (filter (\lst->head lst == item) lists)) sum_all_starting_with3 item lists = sum [sum e | e<-lists, head e==item] -- Problem 5 mystery things = foldr1 (\l1 l2->if (help l1 > help l2) then l1 else l2) things where help [] = 0 help [_] = 1 help (n:m:ns) | n == m = 1 + help (m:ns) | otherwise = 1 {- a) help "exam" --> 1 b) help "aardvark" --> 2 c) mystery ["aardvark", "abbba", "oof"] --> "oof" d) The function selects the word (list) that has the longest sequence of identical characters at the front. The helper function counts the length of these "prefixes", and the foldr1 selects the word with the longest prefix. In the case of a tie, it returns the rightmost item. -}