Skip to content

Instantly share code, notes, and snippets.

@WillNess
Last active December 18, 2015 21:38
Show Gist options
  • Select an option

  • Save WillNess/5848411 to your computer and use it in GitHub Desktop.

Select an option

Save WillNess/5848411 to your computer and use it in GitHub Desktop.
-- http://stackoverflow.com/questions/17242480/determining-ways-of-splitting-change
-- the author writes:
-- assumes the denominations are distinct. If they aren't,
-- ((there will be some dupicates present in the result produced))
change :: (Num a, Ord a) => [a] -> a -> [[a]]
change ds@ ~(d:t) sum
| sum==0 = [[]]
| null ds = []
| otherwise = concat [map (d:) $ change ds $ sum-d | d <= sum] ++ change t sum
-- my comment:
-- `d` is either present in the change, or not. If it is, then we can take
-- one `d` coin out of the change, so it must be possible to change (sum-d).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment