You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('') and can be up to 35 characters long.
38 lines
975 B
38 lines
975 B
(define (square x) (* x x)) 



(define (average x y) 

(/ (+ x y) 2)) 



(define (improve guess x) 

(average guess (/ x guess))) 



(define (goodenough? guess x) 

(< (abs ( (square guess) x)) 0.001)) 



(define (sqrtiter guess x) 

(if (goodenough? guess x) 

guess 

(sqrtiter (improve guess x) x))) 



(define (sqrt x) 

(sqrtiter 1.0 x)) 



;;; this is where things begin fucking up 

;;; it doesn't work because if is a special case 

;;; that uses lazy evaluation, thus it doesn't have to 

;;; evaluate its parameters. 

;;; 

;;; when testing, i found that in guile, it caused a stack overflow; 

;;; whereas in chicken, it caused an infinite loop (maybe). 



(define (newif predicate thenclause elseclause) 

(cond (predicate thenclause) 

(else elseclause))) 



(define (newsqrtiter guess x) 

(newif (goodenough? guess x) 

guess 

(newsqrtiter (improve guess x) x))) 



(display (sqrt 3)) 

;;(display (newsqrtiter 1.0 2))


