Skip to content

Instantly share code, notes, and snippets.

@actsasbuffoon
Created August 26, 2012 00:17
Show Gist options
  • Save actsasbuffoon/3472710 to your computer and use it in GitHub Desktop.
Save actsasbuffoon/3472710 to your computer and use it in GitHub Desktop.
My first attempt at Clojure. Suggestions/criticism welcome.
(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