Created
January 4, 2011 21:42
-
-
Save ashmoran/765488 to your computer and use it in GitHub Desktop.
SICP exercise 1.22
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 (expect actual expectation expected) | |
| (display "• ") | |
| (display actual) | |
| (display " should equal ") | |
| (display expected) | |
| (display ": ") | |
| (expectation actual expected) | |
| (display "pass\n") | |
| ) | |
| (define (to-equal actual expected) | |
| (assert (= actual expected))) | |
| (define (to-be actual expected) | |
| (if expected | |
| (assert actual) | |
| (assert (not actual)))) | |
| (define (prime? n) | |
| (= n (smallest-divisor n))) | |
| (define (smallest-divisor n) | |
| (find-divisor n 2)) | |
| (define (find-divisor n test-divisor) | |
| (cond ((> (square test-divisor) n) n) | |
| ((divides? test-divisor n) test-divisor) | |
| (else (find-divisor n (+ test-divisor 1))))) | |
| (define (divides? a b) | |
| (= (remainder b a) 0)) | |
| (define (square n) | |
| (* n n)) | |
| (expect (prime? 2) to-be #t) | |
| (expect (prime? 3) to-be #t) | |
| (expect (prime? 4) to-be #f) | |
| (expect (prime? 5) to-be #t) | |
| (expect (prime? 6) to-be #f) | |
| (expect (prime? 7) to-be #t) | |
| (expect (prime? 8) to-be #f) | |
| (expect (prime? 9) to-be #f) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment