Skip to content

Instantly share code, notes, and snippets.

(define (square x)
(* x x))
(define (sum-of-squares x y)
(+ (square x) (square y)))
(define (sum-of-highest-squares x y z)
(cond ((< x y z) (sum-of-squares y z))
((< y z x) (sum-of-squares z x))
((< z x y) (sum-of-squares x y))))
@kouddy
kouddy / SICP 1.4
Last active August 29, 2015 14:15
;;;Re-written version of the code
(define (a-plus-abs-b a b)
(if (> b 0)
(+ a b)
(- a b)))
(test 0 p)
;;;Applicative-order
(test 0 p) ;;; Substitute p with p
(test 0 p) ;;; Substitute p with p
(test 0 p) ;;; Substitute p with p
;;;Normal order
(test 0 p)
(if (= 0 0) 0 p) ;;;p's value not needed
@kouddy
kouddy / SICP 1.7
Last active August 29, 2015 14:15
(define (square x) (* x x))
(define (average x y)
(/ (+ x y) 2))
(define (improve guess x)
(average guess (/ x guess)))
;;; Newly implemented guess
;;; Good enough when |guess - improved-guess|/guess < 0.001 (0.1%)
(define (square x) (* x x))
(define (improve guess x)
(/ (+ (/ x (square guess)) (* 2 guess)) 3))
(define (good-enough? guess improved-guess x)
(< (/ (abs (- guess improved-guess)) guess) 0.001))
(define (cubic-root-iter guess x)
(if (good-enough? guess (improve guess x) x)
@kouddy
kouddy / SICP 1.9
Last active August 29, 2015 14:15
;;; First process
(+ 4 5)
(inc (+ 3 5))
(inc (inc (+ 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)
@kouddy
kouddy / SICP 1.10.part 1
Last active August 29, 2015 14:15
Answer for SICP exercise 1.10 Part 1
;;;(A 1 10)
(A 0 (A 1 9)
(* 2 (A 0 (A 1 8)))
(* 2 (* 2 (A 0 (A 1 7))))
(* 2 (* 2 (* 2 (A 0 (A 1 6)))))
(* 2 (* 2 (* 2 (* 2 (A 0 (A 1 5))))))
(* 2 (* 2 (* 2 (* 2 (* 2 (A 0 (A 1 4)))))))
(* 2 (* 2 (* 2 (* 2 (* 2 (* 2 (A 0 (A 1 3))))))))
(* 2 (* 2 (* 2 (* 2 (* 2 (* 2 (* 2 (A 0 (A 1 2)))))))))
(* 2 (* 2 (* 2 (* 2 (* 2 (* 2 (* 2 (* 2 (A 0 (A 1 1))))))))))
;;;(A 2 4)
;;; We know that (A 1 n) = 2^n, so steps to evaluate (A 1 n) are omitted here.
(A 1 (A 2 3))
(A 1 (A 1 (A 2 2)))
(A 1 (A 1 (A 1 (A 2 1))))
(A 1 (A 1 (A 1 2)))
...
(A 1 (A 1 4))
...
(A 1 16)
;;;(A 3 3)
;;; We know that (A 1 n) = 2^n, so steps to evaluate (A 1 n) are omitted here.
;;; We also know that (A 2 n) = 2^2^...n times, so steps to evaluate (A 2 n) are omitted here.
(A 3 3)
(A 2 (A 3 2))
(A 2 (A 2 (A 3 1)))
(A 2 (A 2 2))
...
(A 2 4)
...
(define (f n)
(if (< n 3)
n
(+ (f (- n 1))
(* 2 (f (- n 2)))
(* 3 (f (- n 3))))))