Last active
August 29, 2015 14:20
-
-
Save srid/dca5ee92bcab1d6cefae to your computer and use it in GitHub Desktop.
laundryCoins.hs
This file contains 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
#!/usr/bin/env runhaskell | |
module Main where | |
data Coin = Quarter | Loonie deriving (Eq, Show) | |
type Cents = Int | |
cents :: Coin -> Cents | |
cents Quarter = 25 | |
cents Loonie = 100 | |
cycleCost :: [Coin] | |
cycleCost = concat . replicate 2 $ [Quarter, Quarter, Loonie] | |
nDaysCost :: Int -> [Coin] | |
nDaysCost n = foldr (++) [] $ replicate n cycleCost | |
sumCost :: [Coin] -> Cents | |
sumCost = foldr (+) 0 . map cents | |
loonies :: [Coin] -> Int | |
loonies = length . filter (==Loonie) | |
quarters :: [Coin] -> Int | |
quarters = length . filter (==Quarter) | |
showCoinsNeeded :: Int -> String | |
showCoinsNeeded n = "To do laundry " ++ show n ++ " times, ask the cashier for:\n" ++ show l ++ " loonies,\n" ++ show q ++ " quarters, from\n$" ++ show t ++ " cash back" where | |
cost = nDaysCost n | |
l = loonies cost | |
q = quarters cost | |
t = (sumCost cost) `div` 100 -- For our case, cycle costs rounds to dollars, so we ignore the mod. | |
main :: IO () | |
main = putStrLn $ showCoinsNeeded 3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment