Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Task: a function that takes a list of names and phone numbers i.e.
- -- a list of tuples (or an empty list) and a string (including an
- -- empty one) and returns the values of the matching tuple i.e.
- -- containging the argument string. The result should be a string
- -- containing the values. The function should warn about empty strings
- -- or lists.
- phoneList =
- [ ("Betty", "555-2938")
- , ("Bonnie", "452-2928")
- , ("Patsy", "493-2928")
- , ("Lucille", "205-2928")
- , ("Wendy", "939-8282")
- , ("Penny", "853-2492")
- ]
- findNumber :: [([Char], [Char])] -> [Char] -> [Char]
- findNumber [] _ = "No list given"
- findNumber _ [] = "No name given"
- findNumber list name = aux list name
- where
- aux (x:xs) n
- | n == fst x = fst x ++ ": " ++ snd x ++ "."
- | null xs = "Not on the list"
- | otherwise = aux xs n
- main :: IO ()
- main =
- print $
- map
- (findNumber phoneList)
- ["Betty", "Bonnie", "Patsy", "Lucille", "Wendy", "Penny"]
- -- ["Betty: 555-2938.","Bonnie: 452-2928.","Patsy: 493-2928.","Lucille: 205-2928.","Wendy: 939-8282.","Penny: 853-2492."]
- -- findNumber [] "Betty"
- -- "No list given"
- -- findNumber phoneList ""
- -- "No name given"
- -- findNumber phoneList "Sally"
- -- "Not on the list"
Add Comment
Please, Sign In to add comment