Commit 49844716 authored by bbguimaraes's avatar bbguimaraes
Browse files

sicp: section 1.1

parent 6eef68bd
(display 486)
(newline)
(display (+ 137 349))
(newline)
(display (- 1000 334))
(newline)
(display (* 5 99))
(newline)
(display (/ 10 5))
(newline)
(display (+ 2.7 10))
(newline)
(display (+ 21 35 12 7))
(newline)
(display (* 25 4 12))
(newline)
(display (+ (* 3 5) (- 10 6)))
(newline)
(display (+ (* 3 (+ (* 2 4) (+ 3 5))) (+ (- 10 7) 6)))
(newline)
(display
(+ (* 3
(+ (* 2 4)
(+ 3 5)))
(+ (- 10 7)
6)))
(newline)
(define size 2)
(display size)
(newline)
(display (* 5 size))
(newline)
(define pi 3.14159)
(define radius 10)
(display (* pi (* radius radius)))
(newline)
(define circumference (* 2 pi radius))
(display circumference)
(newline)
(define (square x) (* x x))
(display (square 21))
(newline)
(display (square (+ 2 5)))
(newline)
(display (square (square 3)))
(newline)
(define (sum-of-squares x y)
(+ (square x) (square y)))
(display (sum-of-squares 3 4))
(newline)
(define (f a)
(sum-of-squares (+ a 1) (* a 2)))
(display (f 5))
(newline)
(define (abs x)
(cond ((> x 0) x)
((= x 0) 0)
((< x 0) (- x))))
(display (abs 1))
(newline)
(display (abs 0))
(newline)
(display (abs -1))
(newline)
(define (abs x)
(cond ((< x 0) (- x))
(else x)))
(newline)
(display (abs 1))
(newline)
(display (abs 0))
(newline)
(display (abs -1))
(newline)
(define (abs x)
(if (< x 0)
(- x)
x))
(newline)
(display (abs 1))
(newline)
(display (abs 0))
(newline)
(display (abs -1))
(newline)
(newline)
(define x 7)
(display (and (> x 5) (< x 10)))
(newline)
(define x 42)
(display (and (> x 5) (< x 10)))
(newline)
(define (>= x y)
(or (> x y) (= x y)))
(newline)
(display (>= 10 5))
(newline)
(display (>= 5 10))
(newline)
(define (>= x y)
(not (< x y)))
(newline)
(display (>= 10 5))
(newline)
(display (>= 5 10))
(newline)
(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))
(display (sqrt 9))
(newline)
(display (sqrt (+ 100 37)))
(newline)
(display (sqrt (+ (sqrt 2) (sqrt 3))))
(newline)
(display (square (sqrt 1000)))
(newline)
(define (sqrt x)
(define (good-enough? guess x)
(< (abs (- (square guess) x)) 0.001))
(define (average x y)
(/ (+ x y) 2))
(define (improve guess x)
(average guess (/ x guess)))
(define (sqrt-iter guess x)
(if (good-enough? guess x)
guess
(sqrt-iter (improve guess x) x)))
(sqrt-iter 1.0 x))
(display (sqrt 9))
(newline)
(display (sqrt (+ 100 37)))
(newline)
(display (sqrt (+ (sqrt 2) (sqrt 3))))
(newline)
(display (square (sqrt 1000)))
(newline)
(define (sqrt x)
(define (good-enough? guess)
(< (abs (- (square guess) x)) 0.001))
(define (average x y)
(/ (+ x y) 2))
(define (improve guess)
(average guess (/ x guess)))
(define (sqrt-iter guess)
(if (good-enough? guess)
guess
(sqrt-iter (improve guess))))
(sqrt-iter 1.0))
(newline)
(display (sqrt 9))
(newline)
(display (sqrt (+ 100 37)))
(newline)
(display (sqrt (+ (sqrt 2) (sqrt 3))))
(newline)
(display (square (sqrt 1000)))
(newline)
(display 10)
(newline)
(display (+ 5 3 4))
(newline)
(display (- 9 1))
(newline)
(display (/ 6 2))
(newline)
(display (+ (* 2 4) (- 4 6)))
(define a 3)
(define b (+ a 1))
(newline)
(display (+ a b (* a b)))
(newline)
(display (= a b))
(newline)
(display
(if (and (> b a) (< b (* a b)))
b
a))
(newline)
(display
(cond ((= a 4) 6)
((= b 4) (+ 6 7 a))
(else 25)))
(newline)
(display (+ 2 (if (> b a) b a)))
(newline)
(display
(* (cond ((> a b) a)
((< a b) b)
(else -1))
(+ a 1)))
(display
(/ (+ 5 4 (- 2 (- 3 (+ 6 (/ 4 5)))))
(* 3 (- 6 2) (- 2 7))))
(define (sum-of-squares x y z)
(+ (* x x) (* y y) (* z z)))
(display (sum-of-squares 1 2 3))
(define (square x) (* x x))
(define (sum-of-squares x y z)
(+ (square x) (square y) (square z)))
(newline)
(display (sum-of-squares 1 2 3))
(define (a-plus-abs-b a b)
((if (> b 0) + -) a b))
(display (a-plus-abs-b 1 2))
(newline)
(display (a-plus-abs-b 1 -2))
(define (p) (p))
(define (test x y)
(if (= x 0)
0
y))
(display (test 0 (p)))
(define (new-if predicate then-clause else-clause)
(cond (predicate then-clause)
(else else-clause)))
(display (new-if (= 2 3) 0 5))
(newline)
(display (new-if (= 1 1) 0 5))
(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))
(newline)
(display (sqrt 9))
(define (sqrt-iter guess x)
(define new-guess (improve guess x))
(if (good-enough? guess new-guess)
new-guess
(sqrt-iter new-guess x)))
(define (improve guess x)
(average guess (/ x guess)))
(define (average x y)
(/ (+ x y) 2))
(define (good-enough? old-guess guess)
(< (abs (- guess old-guess)) 0.001))
(define (sqrt x)
(sqrt-iter 1.0 x))
(display (sqrt 9))
(newline)
(display (sqrt (+ 100 37)))
(newline)
(display (sqrt (+ (sqrt 2) (sqrt 3))))
(newline)
(display (square (sqrt 1000)))
(newline)
(define (cbrt-iter guess x)
(define new-guess (improve guess x))
(if (good-enough? guess new-guess)
new-guess
(cbrt-iter new-guess x)))
(define (improve guess x)
(/ (+ (/ x (* guess guess))
(* 2 guess))
3))
(define (good-enough? old-guess guess)
(< (abs (- guess old-guess)) 0.001))
(define (cbrt x)
(cbrt-iter 1.0 x))
(display (cbrt 9))
(newline)
(display (cbrt (+ 100 37)))
(newline)
(display (cbrt (+ (cbrt 2) (cbrt 3))))
(newline)
(display (square (cbrt 1000)))
(newline)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment