Created
May 26, 2015 14:20
-
-
Save shigemk2/5e955ae575a0a02605af to your computer and use it in GitHub Desktop.
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
(define (three a b c) | |
(cond ((and (< a b) (< a c)) (+ (* b b) (* c c))) | |
((and (< b a) (< b c)) (+ (* a a) (* c c))) | |
(else (+ (* a a) (* b b))))) | |
(print (three 3 4 5)) | |
(print (three 5 4 3)) | |
(print (three 4 3 5)) |
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
(define (sqrt-iter guess x) | |
(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 (sqrt x) | |
(sqrt-iter 1.0 x)) | |
;; | |
(print (sqrt 9)) | |
(print (sqrt (+ 100 37))) | |
(print (sqrt (+ (sqrt 2) (sqrt 3)))) | |
(print (square (sqrt 1000))) | |
(print (sqrt 0.000002)) | |
(print (sqrt 200000000000000000000)) |
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
(define (new-if predicate then-clause else-clause) | |
(cond (predicate then-clause) | |
(else else-clause))) | |
(define (sqrt-iter guess x) | |
(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 (sqrt x) | |
(sqrt-iter 1.0 x)) | |
(print (sqrt 9)) | |
(print (sqrt (+ 100 37))) | |
(print (sqrt (+ (sqrt 2) (sqrt 3)))) | |
(print (square (sqrt 1000))) |
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
#?=(+ 5 3 4) | |
#?=(- 9 1) | |
#?=(/ 6 2) | |
#?=(+ (* 2 4) (- 4 6)) | |
(define a 3) | |
(define b (+ a 1)) | |
#?=(+ a b (* a b)) | |
#?=(= a b) | |
#?=(if (and (> b a) (< b (* a b))) | |
b | |
a) | |
#?=(cond ((= a 4) 6) | |
((= b 4) (+ 6 7 a)) | |
(else 25)) | |
#?=(+ 2 (if (> b a) b a)) | |
#?=(* (cond ((> a b) a) | |
((< a b) b) | |
(else -1)) | |
(+ a 1)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment