Last active
January 17, 2018 19:53
-
-
Save sichacvah/4e526a49e5c890aaf319e084121f7a7a to your computer and use it in GitHub Desktop.
SICP 1.12 pascal triangle
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
| (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 kinds-of-coins) | |
| (cond ((= kinds-of-coins 1) 1) | |
| ((= kinds-of-coins 2) 5) | |
| ((= kinds-of-coins 3) 10) | |
| ((= kinds-of-coins 4) 25) | |
| ((= kinds-of-coins 5) 50))) | |
| (define (pascal-triangle row col) | |
| (cond ((< row col) 0) | |
| ((or (= col 0) (= row 0) (= col row)) 1) | |
| (else (+ (pascal-triangle (- row 1) (- col 1)) | |
| (pascal-triangle (- row 1) col))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment