banovski

Phone list search

Apr 12th, 2022 (edited)
401
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Task: a function that takes a list of names and phone numbers i.e.
  2. -- a list of tuples (or an empty list) and a string (including an
  3. -- empty one) and returns the values of the matching tuple i.e.
  4. -- containging the argument string. The result should be a string
  5. -- containing the values. The function should warn about empty strings
  6. -- or lists.
  7.  
  8. phoneList =
  9.   [ ("Betty", "555-2938")
  10.   , ("Bonnie", "452-2928")
  11.   , ("Patsy", "493-2928")
  12.   , ("Lucille", "205-2928")
  13.   , ("Wendy", "939-8282")
  14.   , ("Penny", "853-2492")
  15.   ]
  16.  
  17. findNumber :: [([Char], [Char])] -> [Char] -> [Char]
  18. findNumber [] _ = "No list given"
  19. findNumber _ [] = "No name given"
  20. findNumber list name = aux list name
  21.   where
  22.     aux (x:xs) n
  23.       | n == fst x = fst x ++ ": " ++ snd x ++ "."
  24.       | null xs = "Not on the list"
  25.       | otherwise = aux xs n
  26.  
  27. main :: IO ()
  28. main =
  29.   print $
  30.   map
  31.     (findNumber phoneList)
  32.     ["Betty", "Bonnie", "Patsy", "Lucille", "Wendy", "Penny"]
  33.  
  34. -- ["Betty: 555-2938.","Bonnie: 452-2928.","Patsy: 493-2928.","Lucille: 205-2928.","Wendy: 939-8282.","Penny: 853-2492."]
  35.  
  36. -- findNumber [] "Betty"
  37. -- "No list given"
  38.  
  39. -- findNumber phoneList ""
  40. -- "No name given"
  41.  
  42. -- findNumber phoneList "Sally"
  43. -- "Not on the list"
Add Comment
Please, Sign In to add comment