Skip to content

Instantly share code, notes, and snippets.

@haze
Created September 24, 2017 06:17
Show Gist options
  • Save haze/09716ada808f7a4337d5ca5856d825ef to your computer and use it in GitHub Desktop.
Save haze/09716ada808f7a4337d5ca5856d825ef to your computer and use it in GitHub Desktop.
replaceNth :: (Eq t1, Num t1) => t1 -> t -> [t] -> [t]
replaceNth n b (x:xs)
| n == 0 = b:xs
| otherwise = x:replaceNth (n - 1) b xs
giveChange :: (Ord a, Num a, Num t) => a -> [a] -> [t] -> (a, [(Double, t)])
giveChange a c u
| null c = (a, zip coins u)
| last c <= a = giveChange (a - last c) c (replaceNth l ((u !! l) + 1) u)
| otherwise = giveChange a (init c) u
where l = length c - 1
giveChange' :: (Num t, Num b, Ord b) => b -> [b] -> (b, [(Double, t)])
giveChange' a c = giveChange a c (map (const 0) c)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment