public
anonymous / changeCalc.hs
Created

Challenge #119

  • Download Gist
changeCalc.hs
Haskell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
 
-- Types used
data Coin = Quarter | Dime | Nickle | Penny deriving (Show, Eq)
type CoinBag = [Coin]
 
-- variables
allCoins :: CoinBag
allCoins = [Quarter,Dime,Nickle,Penny]
 
-- Pretty printing helper
pretty :: CoinBag -> IO ()
pretty bag = do
mapM_ putOne allCoins
where putOne = \coin -> prettyLine coin (countCoins bag coin)
 
prettyLine :: Coin -> Int -> IO ()
prettyLine coin 0 = return ()
prettyLine coin count = putStrLn $ show coin ++ ": " ++ show count
 
countCoins :: CoinBag -> Coin -> Int
countCoins bag coin = length $ filter (== coin) bag
 
-- Determine the value of a Coin or CoinBag
val :: Coin -> Integer
val coin = case coin of
Quarter -> 25
Dime -> 10
Nickle -> 5
Penny -> 1
 
bagVal :: CoinBag -> Integer
bagVal [] = 0
bagVal bag = sum $ map val bag
 
-- Algorithm for determining coins needed
addCoins :: CoinBag -> Integer -> Coin -> CoinBag
addCoins bag maxVal coin =
let potentialBag = coin:bag
in if bagVal potentialBag > maxVal
then bag
else addCoins potentialBag maxVal coin
 
addAllCoins :: CoinBag -> Integer -> CoinBag -> CoinBag
addAllCoins bag maxVal [] = undefined
addAllCoins bag maxVal (coin:otherCoins) =
if bagVal bagWithC == maxVal
then bagWithC
else addAllCoins bagWithC maxVal otherCoins
where bagWithC = addCoins bag maxVal coin
 
-- Helper for reading a Double value and converting to an Integer
getInteger :: IO Integer
getInteger = getLine >>= \x -> return . truncate . (100*) $ (read x :: Double)
 
-- Main method
main :: IO ()
main = do
putStrLn "Enter a value:"
amount <- getInteger
pretty $ addAllCoins [] amount allCoins

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.