Created
June 17, 2018 16:35
-
-
Save tylerlrhodes/72115bbf9f1d652d0b3427f1fdb8b666 to your computer and use it in GitHub Desktop.
sicp1.6 Applicative Order Evaluation and an infinite loop
This file contains 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
#lang sicp | |
;; Applicative order evaluation | |
(define (new-if predicate then-clause else-clause) | |
(cond (predicate then-clause) | |
(else else-clause))) | |
(define (sqrt-iter guess x) | |
(display guess) | |
(newline) | |
(new-if (good-enough? guess x) | |
guess | |
(sqrt-iter (improve guess x) | |
x))) | |
(define (improve guess x) | |
(average guess (/ x guess))) | |
(define (average x y) | |
(/ (+ x y) 2)) | |
(define (good-enough? guess x) | |
(< (abs (- (square guess) x)) 0.001)) | |
(define (square x) | |
(* x x)) | |
(define (sqrt x) | |
(sqrt-iter 1.0 x)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment