Last active
October 29, 2019 15:28
-
-
Save chelseatroy/587231740ef46f5bff3e70b9ef427b83 to your computer and use it in GitHub Desktop.
More Higher Order Procedures
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 (square x) (* x x)) | |
(define (qb x) (* x x x)) | |
(define (average x y) | |
(/ (+ x y) 2)) | |
(define (sq-improve guess for-value) | |
(average guess (/ for-value guess))) | |
(define (qb-improve guess for-value) | |
(/ (+(/ for-value (square guess)) (* 2 guess)) 3)) | |
(define (root x improve-function) | |
(define (good-enough? guess previous-guess) | |
(< (abs (/ (- guess previous-guess) guess)) 0.000001)) | |
(define (root-iter previous-guess) | |
(let ((guess (improve-function previous-guess x))) | |
(if (good-enough? guess previous-guess) | |
guess | |
(root-iter guess) | |
) | |
)) | |
(root-iter 1.0) | |
) | |
(define (qbrt x) | |
(root x qb-improve) | |
) | |
(define (sqrt x) | |
(root x sq-improve) | |
) | |
(qb (qbrt 27)) | |
(square (sqrt 9)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment