Skip to content

Instantly share code, notes, and snippets.

@tylerlrhodes
Created June 17, 2018 16:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tylerlrhodes/72115bbf9f1d652d0b3427f1fdb8b666 to your computer and use it in GitHub Desktop.
Save tylerlrhodes/72115bbf9f1d652d0b3427f1fdb8b666 to your computer and use it in GitHub Desktop.
sicp1.6 Applicative Order Evaluation and an infinite loop
#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