Skip to content

Instantly share code, notes, and snippets.

@minimal
Created August 21, 2014 18:08
Show Gist options
  • Save minimal/e87b51b06e34ed2ed2aa to your computer and use it in GitHub Desktop.
Save minimal/e87b51b06e34ed2ed2aa to your computer and use it in GitHub Desktop.
module Data.PhoneBook where
import Data.List
import Data.Maybe
import Control.Plus (empty)
type Entry = { firstName :: String, lastName :: String, phone :: String }
type PhoneBook = List Entry
showEntry :: Entry -> String
showEntry entry = entry.lastName ++ ", " ++
entry.firstName ++ ": " ++
entry.phone
emptyBook :: PhoneBook
emptyBook = empty
insertEntry :: Entry -> PhoneBook -> PhoneBook
insertEntry = Cons
findEntry :: String -> String -> PhoneBook -> Maybe Entry
findEntry firstName lastName = filter filterEntry >>> head
where
filterEntry :: Entry -> Boolean
filterEntry entry = entry.firstName == firstName && entry.lastName == lastName
-- examples
exampleEntry = { firstName: "John", lastName: "Smith", phone: "555-555-5555" }
exampleBook = insertEntry exampleEntry emptyBook
printEntry firstName lastName book = showEntry <$> findEntry firstName lastName book
book3 = insertEntry {firstName: "chris", lastName: "mcd", phone: "999"} exampleBook
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment