Skip to content

Instantly share code, notes, and snippets.

View BaseCase's full-sized avatar

Casey Brant BaseCase

  • Madison, WI
View GitHub Profile
;Exercise 1.1 - Given the Scheme code, what's the output?
10
;10
(+ 5 3 4)
;12
(- 9 1)
;8
;Exercise 1.2 -
(/ (+ 5 4
(- 2
(- 3
(+ 6
(/ 4 5)))))
(* 3
(- 6 2)
(- 2 7)))
;Exercise 1.3 - Define a procedure that takes three numbers
; and returns the sum of the squares of the two larger numbers.
(define (sum-of-squares a b)
(+ (* a a) (* b b)))
(define (foo a b c)
(cond ((and (>= a c) (>= b c)) (sum-of-squares a b))
((and (>= a b) (>= c b)) (sum-of-squares a c))
((and (>= b a) (>= c a)) (sum-of-squares b c))))
;Exercise 1.4 - Describe the behavior of the following prodcedure:
(define (a-plus-abs-b a b)
((if (> b 0) + -) a b))
;Exercise 1.5 - Applicative vs normal-order evaluation.
;given this code:
(define (p) (p))
(define (test x y)
(if (= x 0)
0
y))
;what happens when we evaluate this?
;first snippet
(define (+ a b)
(if (= a 0)
b
(inc (+ (dec a) b))))
;second snippet
(define (+ a b)
(if (= a 0)
b
;Exercise 1.11 - Given a function f, define two versions of
; a procedure that computes f, one which generates a
; tree recursive process, and one that generates an
; iterative process.
;recursive version:
(define (f n)
(if (< n 3)
n
(+ (f (- n 1))
;Exercise 1.12 - Write a procedure that computes elements of
; Pascal's Triangle via a recursive process.
(define (pascal row col)
(if (or (= row col) (= col 1))
1
(+ (pascal (- row 1) (- col 1)) (pascal (- row 1) col))))
;Exercise 1.29- Define a procedure that uses Simpson's Rule to compute the integral
; of a function. Also, compare this proc with another one in the text.
;to do this one, we need the cube procedure from the text:
(define (cube x)
(* x x x))
;and also the sum procedure:
(define (sum term a next b)
(if (> a b)
(define (cons x y)
(define (dispatch m)
(cond ((= m 0) x)
((= m 1) y)
(else (error "Argument not 0 or 1"))))
dispatch)