[Haskell-beginners] A try on a bank machine algorithm...

Brent Yorgey byorgey at seas.upenn.edu
Mon Jun 1 12:19:47 EDT 2009


On Mon, Jun 01, 2009 at 10:55:30AM -0400, Thomas Friedrich wrote:
>
> Ok, I don't really think I know what you are planing to do. Say I'd like to 
> have 175 € form an ATM and [200, 100, 50, 20, 10,5] is the list of bills 
> that can be used to cash this money. Do you what to produce a list like 
> [0,1,1,1,0,1]? Telling you that you have to take one 100€ bill, one 50€ 
> bill, etc...
>
> Then I would do it the following:
>
> cashout :: (Integral a) => a -> [a] -> [a]
> cashout 0 _ = []
> cashout _ [] = error "*** in cashout: Not cashable."
> cashout tocash (b:bs) = fst r : cashout (snd r) bs
> where r = tocash `divMod` b
>
> prop_cashout tocash bills = tocash == sum (zipWith (*) (cashout tocash 
> bills) bills)
>
>
> Hence,
>
> cashout 175 [200,100,50,20,10,5] == [0,1,1,1,0,1]
>
> But maybe you want something different.

It sounded to me a little different: like if you withdraw 200 €, you
don't just want a 200 € bill, you want some small bills too: so you
would get two 5s, two 10s, a 20, a 50, and a 100.

-Brent


More information about the Beginners mailing list