Last active
November 23, 2015 11:31
-
-
Save hellerve/87b761af4d5e6b23a4f7 to your computer and use it in GitHub Desktop.
This is a zepto solution to a problem described in SICP 1.2.2
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
;; Solution to: | |
;; How many different ways can we make change of $ 1.00, given half-dollars, quarters, dimes, nickels, and pennies? | |
;; More generally, can we write a procedure to compute the number of ways to change any given amount of money? | |
;; | |
;; Example usage for $1.00: (count-change 100) => 292 | |
;; This computation takes around 0.6 secs on my machine in zepto. | |
(define (count-change amount) | |
(cc amount 5)) | |
(define (cc amount kinds-of-coins) | |
(cond ((= amount 0) 1) | |
((or (< amount 0) (= kinds-of-coins 0)) 0) | |
(else (+ (cc amount | |
(- kinds-of-coins 1)) | |
(cc (- amount | |
(first-denomination kinds-of-coins)) | |
kinds-of-coins))))) | |
(define first-denomination (hash-map [1 1] [2 5] [3 10] [4 25] [5 50])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment