Friedrich Dominicus wrote:

> But again I'm wondering if this:
>
> makeChange :: Int -> [Int] -> [Int]
> makeChange 0 (c:coins) = 0:makeChange 0 coins
> makeChange 0 [] = []
> makeChange n (coin_val:coins) = let (how_many,rest) = divMod n coin_val
>        in
>        how_many:makeChange rest coins
>
> could be replaces by some combination of HOFs. Could someone give me a
> hand here?

How about this:

makeChanges total coins =
  tail $ map fst $ scanl (\(how_many,rest) -> divMod rest) (0,total) coins

But I doubt this is easier to understand.

BTW, your first two branches can be combined:

makeChange 0 = map (const 0)

-- Zhanyong Wan



Reply via email to