Created
October 10, 2009 22:19
-
-
Save russelldb/207194 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -module(change). | |
| -compile(export_all). | |
| count_change(Amount, CoinSet) -> | |
| FList = lists:sort(CoinSet), | |
| Fd = fun(X) -> | |
| lists:nth(X, FList) end, | |
| cc(Amount, length(CoinSet), Fd). | |
| cc(0, _C, _Fd) -> 1; | |
| cc(N, C, _Fd) when N < 0 ; C =:= 0-> 0; | |
| cc(Amount, Coins, Fd) -> | |
| cc(Amount, Coins-1, Fd) + cc(Amount - Fd(Coins), Coins, Fd). | |
| %% Thanks http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-11.html#%_sec_1.2.2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment