Skip to content

Instantly share code, notes, and snippets.

@srid
Last active August 29, 2015 14:20
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 srid/dca5ee92bcab1d6cefae to your computer and use it in GitHub Desktop.
Save srid/dca5ee92bcab1d6cefae to your computer and use it in GitHub Desktop.
laundryCoins.hs
#!/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