Created
January 7, 2011 23:59
-
-
Save ashmoran/770339 to your computer and use it in GitHub Desktop.
SICP exercise 1.35
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-close actual expected) | |
| (assert (< (abs (- actual expected)) 0.0001))) | |
| (define (to-be actual expected) | |
| (if expected | |
| (assert actual) | |
| (assert (not actual)))) | |
| (define tolerance 0.00001) | |
| (define (fixed-point f first-guess) | |
| (define (close-enough? v1 v2) | |
| (< (abs (- v1 v2)) tolerance)) | |
| (define (try guess) | |
| (let ((next (f guess))) | |
| (if (close-enough? guess next) | |
| next | |
| (try next)))) | |
| (try first-guess)) | |
| (expect (fixed-point cos 1.0) to-be-close 0.7391) | |
| (expect (fixed-point (lambda (x) (+ 1 (/ 1 x))) 1.0) to-be-close 1.61803399) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment