Created
August 26, 2012 00:17
-
-
Save actsasbuffoon/3472710 to your computer and use it in GitHub Desktop.
My first attempt at Clojure. Suggestions/criticism welcome.
This file contains 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
(defn calculate-change | |
([paid cost] (make-change paid cost 0)) | |
([paid cost denomination-index] | |
(let | |
[ | |
currency-values [ | |
[:hundreds 10000] | |
[:fifties 5000] | |
[:twenties 2000] | |
[:tens 1000] | |
[:fives 500] | |
[:ones 100] | |
[:quarters 25] | |
[:dimes 10] | |
[:nickels 5] | |
[:pennies 1] | |
] | |
] | |
(if | |
(>= denomination-index (count currency-values)) {} | |
(let | |
[ | |
denomination (currency-values denomination-index) | |
amount (int (/ (- paid cost) (last denomination))) | |
value (* amount (last denomination)) | |
] | |
(conj | |
{(first denomination) amount} | |
(make-change (- paid value) cost (+ denomination-index 1)) | |
) | |
) | |
) | |
) | |
) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment