home *** CD-ROM | disk | FTP | other *** search
- -- Let's represent coins by their integer values:
-
- type Coin = Int
-
- -- Denominations of coins in current use in the UK:
-
- coins :: [Coin]
- coins = reverse (sort [1, 2, 5, 10, 20, 50, 100])
-
- -- Find all possible (decreasing sequences) of change for given amount:
-
- change :: Int -> [[Coin]]
- change 0 = [[]]
- change amount = [ c:cs | c<-coins, amount>=c, cs<-change (amount - c) ]
-
- -- Find optimal change for given amount:
- -- intended semantics: change' = head . change
-
- change' :: Int -> [Coin]
- change' 0 = []
- change' amount = coin : change' (amount - coin)
- where coin = head [ c | c<-coins, c<=amount ]
-
-
-