Skip to content

Instantly share code, notes, and snippets.

@plashchynski
Last active March 25, 2020 23:48
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 plashchynski/81be79ead9727b244c57ad4eb4f4d1f6 to your computer and use it in GitHub Desktop.
Save plashchynski/81be79ead9727b244c57ad4eb4f4d1f6 to your computer and use it in GitHub Desktop.

1.3

(define (proc x1 x2 x3)
        (cond   ((and (> x1 x3) (> x2 x3)) (+ (* x1 x1) (* x2 x2)))
                ((and (> x1 x2) (> x3 x2)) (+ (* x1 x1) (* x3 x3)))
                ((and (> x2 x1) (> x3 x1)) (+ (* x2 x2) (* x3 x3)))
        )
)

1.7

(define (sqrt-iter guess old_guess x)
  (if (good-enough? guess old_guess)
      guess
      (sqrt-iter (improve guess x)
                 guess x)))

(define (improve guess x)
  (average guess (/ x guess)))

(define (average x y)
  (/ (+ x y) 2))
  
(define (good-enough? guess old_guess)
  (< (abs (- guess old_guess)) 0.001))

(define (sqrt x)
  (sqrt-iter 1.0 x x))

1.8

(define (cube-root-iter guess old_guess x)
  (if (good-enough? guess old_guess)
      guess
      (cube-root-iter (improve guess x)
                 guess x)))

(define (improve guess x)
  (/ (+ (/ x (* 2 guess)) (* 2 guess)) 3))

(define (average x y)
  (/ (+ x y) 2))
  
(define (good-enough? guess old_guess)
  (< (abs (- guess old_guess)) 0.001))

(define (cube-root x)
  (cube-root-iter 1.0 x x))

1.9

(define (+ a b)
  (if (= a 0)
      b
      (inc (+ (dec a) b))))

(+ 4 5)

(inc (+ (dec 4) 5))
(inc (+ 3 5))
(inc (inc (+ (dec 3) 5)))
(inc (inc (+ 2 5)))
(inc (inc (inc (+ (dec 2) 5))))
(inc (inc (inc (+ 1 5))))
(inc (inc (inc (inc (+ 0 5)))))
(inc (inc (inc (inc 5))))
(inc (inc (inc 6)))
(inc (inc 7))
(inc 8)
9

(define (+ a b)
  (if (= a 0)
      b
      (+ (dec a) (inc b))))

(+ 4 5)

(+ (dec a) (inc b))
(+ (dec 4) (inc 5))
(+ 3 6)
(+ (dec 3) (inc 6))
(+ 2 7)
(+ (dec 2) (inc 7))
(+ 1 8)
(+ (dec 1) (inc 8))
(+ 0 9)
9

1.9

(define (A x y)
  (cond ((= y 0) 0)
        ((= x 0) (* 2 y))
        ((= y 1) 2)
        (else (A (- x 1)
                 (A x (- y 1))))))

(A (- x 1) (A x (- y 1))

(A 1 10)
(A 0 (A 1 9))
(A 0 (A 0 (A 1 8))
(A 0 (A 0 (A 0 (A 1 7
(A 0 (A 0 (A 0 (A 0 (A 1 6
(A 0 (A 0 (A 0 (A 0 (A 0 (A 1 5
(A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 1 4
(A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 1 3
(A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 1 2
(A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 1 1
(A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 2
(A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 4
(A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 8
(A 0 (A 0 (A 0 (A 0 (A 0 (A 0 16
(A 0 (A 0 (A 0 (A 0 (A 0 32
(A 0 (A 0 (A 0 (A 0 64
(A 0 (A 0 (A 0 128
(A 0 (A 0 256
(A 0 512
1024

(A (- x 1) (A x (- y 1)))

(A 2 4)
(A (- 2 1) (A 2 (- 4 1)))
(A 1 (A 2 3))
(A 1 (A (- 2 1) (A 2 (- 3 1))))
(A 1 (A 1 (A 2 2)))
(A 1 (A 1 (A (- 2 1) (A 2 (- 2 1)))))
(A 1 (A 1 (A 1 (A 2 1))))
(A 1 (A 1 (A 1 2)))
(A 1 (A 1 (A 0 (A 1 1))))
(A 1 (A 1 (A 0 2)))
(A 1 (A 1 4))
(A 1 (A (- 1 1) (A 1 (- 4 1))))
(A 1 (A 0 (A 1 3)))
(A 1 (A 0 (A (- 1 1) (A 1 (- 3 1)))))
(A 1 (A 0 (A 0 (A 1 2))))
(A 1 (A 0 (A 0 (A (- 1 1) (A 1 (- 2 1))))))
(A 1 (A 0 (A 0 (A 0 (A 1 1)))))
(A 1 (A 0 (A 0 (A 0 2))))
(A 1 (A 0 (A 0 4)))
(A 1 (A 0 8))
(A 1 16)
(A 1 (A (- 1 1) (A 1 (- 16 1))))
(A 1 (A 0 (A 1 15)))
(A 1 (A 0 (A (- 1 1) (A 1 (- 15 1)))))
(A 1 (A 0 (A 0 (A 1 14))))
...
...
...
65536

(define (f n) (A 0 n))

(define (g n) (A 1 n))

(define (h n) (A 2 n))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment