If I have:
data Data = T | F
deriving (Eq, Show, Ord)
type PhoneBook = [(String, Data)]
testBook :: Book
testBook = [("aT", T), ("bF", F)]
I’m trying to make a look up function, but because I’m using a user defined type it’s throwing me off. Any suggestions? I’m trying:
lookup1 :: PhoneBook->String->Data
lookup1 [] key = Nothing
lookup1 ((k,v):xs) key = if key == k
then Just v
else lookup1 xs key
When you have a statement like
type Book = [(String, Answer)]thenBookis just a synonym for[(String, Answer)]. This means that you can replaceBookwith[(String, Answer)]anywhere in your types without changing them. This means that looking up a value in aBookis exactly the same as looking up a value in a[(String, Answer)].Thus you can just use the normal
lookupfunction to get the answer corresponding to a particular string.