by Matt Brennan

Making change

How do we make money into change?

It’s nice to know what’s the biggest coint fits in an amount.

fit = (coins, amt)-->
	maximum filter (<= amt), coins

These are our coins, by the way

coins =
	1: 'penny'
	2: 'tuppence'
	5: 'fivepence'
	10: 'ten pence'
	20: 'twenty pence'
	50: 'fifty pence'
	100: 'quid'
	200: 'two quid'

fit-sterling = fit keys coins

So we find the biggest coin that fits

coin = fit amt

And keep doing that with what’s left.

change (amt - coin)

And we don’t need coins to make no change.

amt is 0 => []

We can count a list of coin sizes

collect = count-by (coins.)

All together now

change = (fit, amt)-->
	| amt is 0 => []
	| otherwise => let coin = fit amt
		[coin] ++ change fit, (amt - coin)

collect change fit-sterling, 537
#⇒ 'two quid':2 'quid':1 'twenty':1 'ten':1 'fivepence':1 'tuppence':1