Last active
May 3, 2016 03:06
-
-
Save PandaWhisperer/88c54d5e768715952828e9ae37a82843 to your computer and use it in GitHub Desktop.
SICP Chapter 1 Exercises https://repl.it/CKty/4
This file contains hidden or 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
;;--------------;; | |
;; Exercise 1.1 ;; | |
;;--------------;; | |
10 | |
;=> 10 | |
(+ 5 3 4) | |
;=> 12 | |
(- 9 1) | |
;=> 8 | |
(/ 6 2) | |
;=> 3 | |
(- (* 2 4) (- 4 6)) | |
;=> 10 | |
(+ (* 2 4) (- 4 6)) | |
;=> 6 | |
(define a 3) | |
(define b (+ a 1)) | |
(+ a b (* a b)) | |
;=> 19 | |
(if (and (> b a) (< b (* a b))) | |
b | |
a) | |
;=> 4 | |
(cond ((= a 4) 6) | |
((= b 4) (+ 6 7 a)) | |
(else 25)) | |
;=> 16 | |
(+ 2 (if (> b a) b a)) | |
;=> 6 | |
(* (cond ((> a b) a) | |
((< a b) b) | |
(else -1)) | |
(+ a 1)) | |
;=> 16 | |
;;--------------;; | |
;; Exercise 1.2 ;; | |
;;--------------;; | |
(/ (+ 5 4 (- 2 (- 3 (+ 6 (/ 4 5))))) (* 3 (- 6 2) (- 2 7))) | |
;=> -0.24666666666666667 | |
;;--------------;; | |
;; Exercise 1.3 ;; | |
;;--------------;; | |
(define (sq x) (* x x)) | |
(define (sos x y) (+ (sq x) (sq y))) | |
(define (ex1.4 a b c) | |
(cond ((or (> a b c) (> b a c)) (sos a b)) | |
((or (> a c b) (> c a b)) (sos a c)) | |
((or (> b c a) (> c b a)) (sos b c)))) | |
;; Test | |
(ex1.4 1 2 3) | |
;=> 13 | |
(ex1.4 3 2 1) | |
;=> 13 | |
(ex1.4 2 3 1) | |
;=> 13 | |
;;--------------;; | |
;; Exercise 1.4 ;; | |
;;--------------;; | |
; Given: | |
(define (a-plus-abs-b a b) | |
((if (> b 0) + -) a b)) | |
; Explanation: operators are just functions that can be returned | |
; from expressions. Thus, if b is positive, it will be added to a, | |
; otherwise it will be substracted. | |
;;--------------;; | |
;; Exercise 1.5 ;; | |
;;--------------;; | |
; Given: | |
(define (p) (p)) | |
(define (test x y) | |
(if (= x 0) | |
0 | |
y)) | |
; Observation: (p) is self-referential, and will lead to an infinite loop | |
; if evaluated. Therefore, the expression (test 0 (p)) will cause an infinite | |
; loop if the interpreter evaluates the expression starting from the innermost, | |
; but it will work fine if it is evaluated only as necessary. | |
; TODO: which of these is applicative order and which is normal order? | |
;;--------------;; | |
;; Exercise 1.6 ;; | |
;;--------------;; | |
;Given: | |
(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 (square x) (* x x)) | |
; When redefining `sqrt-iter` using `new-if`, it causes an endless loop. Why? | |
(define (new-if predicate then-clause else-clause) | |
(cond (predicate then-clause) | |
(else else-clause))) | |
;;--------------;; | |
;; Exercise 1.7 ;; | |
;;--------------;; | |
;;--------------;; | |
;; Exercise 1.8 ;; | |
;;--------------;; | |
; Define a function compute a cube root using Newton's method | |
; analogous to the iterative square root function | |
(define (cbrt-iter guess x) | |
(if (cb-good-enough? guess x) | |
guess | |
(cbrt-iter (cb-improve guess x) | |
x))) | |
(define (cb-improve guess x) | |
(/ (+ (/ x (* guess guess)) (* 2 guess)) 3)) | |
(define (cb-good-enough? guess x) | |
(< (abs (- (* guess guess guess) x)) 0.001)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment