Lisp - scheme方言
1 (define (abs x) (if(< x 0) (- x) x)) 2 3 (define (square x) (* x x)) 4 5 (define (average x y) (/ (+ x y ) 2)) 6 7 (define (improve guess x) (average guess (/ x guess))) 8 9 (define (guess-enough? guess x) (< (abs (- (square guess) x)) 0.001)) 10 11 (define (sqrt-iter guess x) (if (guess-enough? guess x) guess (sqrt-iter (improve guess x) x))) 12 13 (define (sqrt x) (sqrt-iter 1.0 x))