Skip to content

Instantly share code, notes, and snippets.

@svdberg
Created January 4, 2011 17:11
Show Gist options
  • Save svdberg/765047 to your computer and use it in GitHub Desktop.
Save svdberg/765047 to your computer and use it in GitHub Desktop.
Haskell implementation of a addressbook
import Text.CSV
type FirstName = String
type LastName = String
type MiddleName = String
type FullName = String
type PhoneNumber = String
data Contact = Contact
{ firstname :: FirstName
, middlename :: MiddleName
, lastname :: LastName
, fullname :: FullName
, phonenumber :: PhoneNumber } deriving (Show, Eq)
type AdressBook = [Contact]
changePhone :: Contact -> PhoneNumber -> Contact
changePhone contact p = Contact fin mn ln fn p
where
fin = firstname contact
mn = middlename contact
ln = lastname contact
fn = fullname contact
addContact :: AdressBook -> Contact -> AdressBook
addContact book con = con:book
isContact :: Contact -> Contact -> Bool
isContact con econ = con == econ
removeContact :: AdressBook -> Contact -> AdressBook
removeContact book con = filter (\x -> not (isContact con x)) book
getContact :: AdressBook -> Contact -> Contact
getContact book con = head list
where
list = filter (\x -> isContact con x) book
nrOfContacts :: AdressBook -> Int
nrOfContacts book = length book
loadAddressBook :: String -> AdressBook
loadAddressBook path = do
csvFile <- parseCSVFromFile path
return (map createContact (fromCSV csvFile))
fromCSV (Left parseError) = error (show parseError)
fromCSV (Right rows) = rows
createContact [ln,mdn,fn,gsm] =
Contact ln mdn fn gsm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment