makeChange :: Int -> [Int] -> [Int]
makeChange _ [] = []
makeChange 0 _ = []
makeChange amt (v:vs) = n : (makeChange newamt vs)
where n = amt `div` v
newamt = amt `mod` v |

makeChange :: Int -> [Int] -> [Int]
makeChange _ [] = []
makeChange 0 _ = []
makeChange amt (v:vs) = n : (makeChange newamt vs)
where n = amt `div` v
newamt = amt `mod` v

This was one tricky to do with higher-order functions, until I discovered `scanl`.

makeChange' :: Int -> [Int] -> [Int]
makeChange' amt vs = zipWith div modvs vs
where modvs = scanl mod amt vs |

makeChange' :: Int -> [Int] -> [Int]
makeChange' amt vs = zipWith div modvs vs
where modvs = scanl mod amt vs

See technical error 12 for a note about this exercise.

This entry was posted on Thursday, July 26th, 2007 at 11:20 pm and is filed under Uncategorized. You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.

Here’s one way of solving the problem as described in the technical error, which finds the best way of doing it. It uses several functions from the Standard Prelude and a little bit of currying. Hopefully this will be properly formatted – apologies if not!

We can do it with what we currently learnt (without scanl):

makeChange amt cs = zipWith div modList cs

where modList = amt : map (mod amt) cs

Without currying:

makeChange amt cs = zipWith div modList cs

where modList = amt : map doMod cs

doMod x = amt `mod` x