PNuts


Joined 2 years ago
Homeworks submitted:
Homework comments:
1
1

About Me

No description provided.

Classes

Structure and Interpretation of Computer Programs

Class status: Established
Role: Student
. 7% complete

Submitted Assignments

Structure and Interpretation of Computer Programs: Lesson 1, HW 1

Solutions provided as code with written answers as comments.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ex-1.1.scm

10
;Value: 10

(+ 5 3 4)
;Value: 12

(- 9 1)
;Value: 8

(/ 6 2)
;Value: 3

(+ (* 2 4) (- 4 6))
;Value: 6

(define a 3)
;Value: a

(define b (+ a 1))
;Value: b

(+ a b (* a b))
;Value: 19

(= a b)
;Value: #f

(if (and (> b a) (< b (* a b)))
    b
    a)
;Value: 4

(cond ((= a 4) 6)
      ((= b 4) (+ 6 7 a))
      (else 25))
;Value: 16

(+ 2 (if (> b a) b a))
;Value: 6

(* (cond ((> a b) a)
         ((< a b) b)
         (else -1))
   (+ a 1))
;Value: 16

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ex-1.2.scm

(/ (+ 5 4 (- 2 (- 3 (+ 6 4/5))))
   (* 3 (- 6 2) (- 2 7)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ex-1.3.scm

(define (sum-of-squares x y)
  (+ (square x) (square y)))

(define (two-largest f a b c)
  (if (and (< a b) (< a c))
      (f b c)
      (if (< b c) (f a c) (f a b))))

(define (two-largest-sum-of-squares a b c)
  (two-largest sum-of-squares a b c))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ex-1.4.scm

(define (a-plus-abs-b a b)
  ((if (> b 0) + -) a b))

; returns a + |b| by using addition when b is positive and subtraction
; when it is negative

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ex-1.5.scm

(define (p) (p))
(define (test x y)
  (if (= x 0)
      0
      y))
(test 0 (p))

; applicative-order evaluation: (p) is evaluated before being
; substituted as a parameter for test, causing infinite recursion

; normal-order evaluation: (p) is only substituted for y in test, but
; is never evaluated as the alternative for the if expression is never
; reached

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ex-1.6.scm

(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)))

; use of new-if results in error message: Aborting!: maximum recursion
; depth exceeded

; the built-in if is a special form, which only evaluates one of the
; consequent and alternative, but calling new-if evaluates both of its
; parameters

(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))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ex-1.7.scm

(define (sqrt-iter guess prev x)
  (if (good-enough? guess prev)
          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 prev)
  (<
   (abs (- 1 (/ guess prev)))
   0.001))

; this form of good-enough? gives more accurate results for small
; numbers, and avoids "Floating-point overflow" for very large numbers

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

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ex-1.8.scm

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

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

(define (cube x)
  (* x x x))

(define (good-enough? guess x)
  (< (abs (- (cube guess) x)) 0.001))

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

PNuts 2 years ago