Created
March 11, 2016 11:37
-
-
Save jakab922/8deb7f8707871b6b6ebe to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- A spinoff of the Vcard.hs example from http://book.realworldhaskell.org/read/programming-with-monads.html | |
import Control.Monad | |
data Context = Home | Mobile | Business | |
deriving (Eq, Show) | |
type Phone = String | |
type Phones = [Phone] | |
albulena = [(Home, "+35-1234")] | |
nils = [ | |
(Home, "1234"), | |
(Business, "456"), | |
(Home, "877788"), | |
(Business, "8748753854")] | |
tumba = [(Mobile, "1757")] | |
onePersonalPhone :: [(Context, Phone)] -> Maybe Phone | |
onePersonalPhone ps = case lookup Home ps of | |
Nothing -> lookup Mobile ps | |
Just n -> Just n | |
allBusinessPhones :: [(Context, Phone)] -> Maybe Phones | |
allBusinessPhones ps = case filter (contextIs Business) ps of | |
[] -> Nothing | |
ns -> return $ map snd ns | |
where contextIs a (b, _) = a == b | |
allPersonalPhones :: [(Context, Phone)] -> Maybe Phones | |
allPersonalPhones ps = filterM (\x -> Just $ Home == fst x) ps >>= mapM (Just . snd) | |
getPhonesOfType ty ps = filterM (\x -> Just $ ty == fst x) ps >>= mapM (Just . snd) | |
main = print . show $ getPhonesOfType Business nils |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment