Skip to content

Instantly share code, notes, and snippets.

@rogeriochaves
Created November 22, 2015 00:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rogeriochaves/95980164ff685c040fbf to your computer and use it in GitHub Desktop.
Save rogeriochaves/95980164ff685c040fbf to your computer and use it in GitHub Desktop.
Contacts Program in Haskell
import Data.List
main = do
putStrLn "Welcome to the contacts list program!"
mainMenu [] -- initial contacts list state
mainMenu :: [String] -> IO b
mainMenu contacts = do
putStrLn "1 - Add new contacts; 2 - Show contacts; 3 - Sort contacts"
option <- getLine
nextContacts <- (chooseOption option contacts)
mainMenu nextContacts
chooseOption :: String -> [String] -> IO [String]
chooseOption op | op == "1" = addContact
| op == "2" = showContacts
| op == "3" = sortContacts
| otherwise = return
addContact :: [String] -> IO [String]
addContact contacts = do
putStrLn "Insert a name"
contact <- getLine
return $ contacts ++ [contact]
showContacts :: [String] -> IO [String]
showContacts contacts = do
putStrLn "Contact list:"
mapM_ putStrLn (map ("> " ++) contacts)
return contacts
sortContacts :: Ord a => [a] -> IO [a]
sortContacts contacts = do
putStrLn "Contacts sorted!"
return $ sort contacts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment