This file contains 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 - Given the Scheme code, what's the output? | |
10 | |
;10 | |
(+ 5 3 4) | |
;12 | |
(- 9 1) | |
;8 |
This file contains 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.2 - | |
(/ (+ 5 4 | |
(- 2 | |
(- 3 | |
(+ 6 | |
(/ 4 5))))) | |
(* 3 | |
(- 6 2) | |
(- 2 7))) |
This file contains 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.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)))) |
This file contains 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.4 - Describe the behavior of the following prodcedure: | |
(define (a-plus-abs-b a b) | |
((if (> b 0) + -) a b)) |
This file contains 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.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? |
This file contains 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
;first snippet | |
(define (+ a b) | |
(if (= a 0) | |
b | |
(inc (+ (dec a) b)))) | |
;second snippet | |
(define (+ a b) | |
(if (= a 0) | |
b |
This file contains 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.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)) |
This file contains 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.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)))) |
This file contains 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.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) |
This file contains 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
(define (cons x y) | |
(define (dispatch m) | |
(cond ((= m 0) x) | |
((= m 1) y) | |
(else (error "Argument not 0 or 1")))) | |
dispatch) |
OlderNewer