Question
Finish the coin changing program by filling in the details for
next_coin and main.
import Data.Functor.Identity
import Control.Monad.Trans.Class
import Control.Monad.State
type Coins = [Int]
type StMach a = StateT Coins IO a
next_coin:: Int -> StMach Int
next_coin amt =
-- finish this
recurs :: Int -> StMach Int
recurs amt =
if amt==0
then return amt
else next_coin amt >>= recurs
dispense :: Int -> StMach ()
dispense i =
( lift $
putStrLn
("dispense "++(show i)++cents)
)
where
cents = if i==1 then " cent" else " cents"
make_change :: Coins -> Int -> IO ()
make_change css amt =
runStateT
(recurs amt) css >> return ()
main =
do putStrLn "Enter an amount: "
-- finish this
You will need a function which converts [Char]s to Ints.
It is called read. Haskell will know which read to use
because you will put it in a context where it gets a [Char] and must
produce an Int.
If you have trouble with the do block in main, remember
it must be an IO do block, not a StateT IO do
block.