home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Datafile PD-CD 2
/
DATAFILE_PDCD2.iso
/
utilities
/
_gofer
/
!Gofer
/
archives
/
Demos
/
gs
/
change
< prev
next >
Wrap
Text File
|
1993-02-12
|
660b
|
25 lines
-- 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 ]