learning-sicp

My embarrassing half assed SICP run.
Log | Files | Refs

commit 2250859ae9c1ad24755ab203e8ed62a5ec7a855d
Author: Yuval Langer <yuval.langer@gmail.com>
Date:   Thu, 19 Jan 2023 05:12:49 +0200

First commit.

Diffstat:
Aguile.scm | 232+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 232 insertions(+), 0 deletions(-)

diff --git a/guile.scm b/guile.scm @@ -0,0 +1,232 @@ +#!/bin/guile \ +--no-auto-compile +!# + +(use-modules (srfi srfi-64)) + +#! + +*Exercise 1.1:* Below is a sequence of expressions. What is the + result printed by the interpreter in response to each expression? + Assume that the sequence is to be evaluated in the order in which + it is presented. +!# + + +10 + +(test-begin "1.1") +(test-equal 10 10) +(test-end "1.1") + +(+ 5 3 4) + +12 + +(- 9 1) + +8 + +(/ 6 2) + +3 + +(+ (* 2 4) (- 4 6)) + +6 + +(define a 3) + +'nothing + +(define b (+ a 1)) + +'nothing + +(+ a b (* a b)) + +19 + +(= a b) + +#f + +(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 + +(define (square a) + (* a a)) + +'nothing + +(define (sum-of-squares a b) + (+ (square a) + (square b))) + +'nothing + +#! + + *Exercise 1.2:* Translate the following expression into prefix + form. + + 5 + 4 + (2 - (3 - (6 + 4/5))) + ----------------------------- + 3(6 - 2)(2 - 7) + +!# + +(test-begin "1.2") +(test-equal + (/ (+ 5 + 4 + (- 2 + (- 3 + (+ 6 + (/ 4 5))))) + (* 3 + (- 6 2) + (- 2 7))) + (/ (- 37) 150)) +(test-end "1.2") + +#! + + *Exercise 1.3:* Define a procedure that takes three numbers as + arguments and returns the sum of the squares of the two larger + numbers. + +!# + +(define (exercise-1-3 a b c) + (if (< a b) + (if (< c a) + (sum-of-squares a b) + (sum-of-squares c b)) + (if (< c b) + (sum-of-squares b a) + (sum-of-squares c a)))) + +(test-begin "1.3") +(test-equal (exercise-1-3 2 3 5) 34) +(test-equal (exercise-1-3 2 5 3) 34) +(test-equal (exercise-1-3 3 2 5) 34) +(test-equal (exercise-1-3 3 5 2) 34) +(test-equal (exercise-1-3 5 2 3) 34) +(test-equal (exercise-1-3 5 3 2) 34) +(test-end "1.3") + +#! + + *Exercise 1.4:* Observe that our model of evaluation allows for + combinations whose operators are compound expressions. Use this + observation to describe the behavior of the following procedure: + + (define (a-plus-abs-b a b) + ((if (> b 0) + -) a b)) + +The result of the "if" expression will be either the procedure "+" or +the procedure "-", which is then applied to "a" and "b". + +!# + +#! + + *Exercise 1.5:* Ben Bitdiddle has invented a test to determine + whether the interpreter he is faced with is using + applicative-order evaluation or normal-order evaluation. He + defines the following two procedures: + + (define (p) (p)) + + (define (test x y) + (if (= x 0) + 0 + y)) + + Then he evaluates the expression + + (test 0 (p)) + + What behavior will Ben observe with an interpreter that uses + applicative-order evaluation? What behavior will he observe with + an interpreter that uses normal-order evaluation? Explain your + answer. (Assume that the evaluation rule for the special form + `if' is the same whether the interpreter is using normal or + applicative order: The predicate expression is evaluated first, + and the result determines whether to evaluate the consequent or + the alternative expression.) + +Applicative-order evaluation: + +(test 0 (p)) +(test 0 (p)) +(test 0 and so on) + +Normal-order evaluation: + +(test 0 (p)) +(if (= 0 0) + 0 + (p)) +(if #t + 0 + (p)) +0 + +!# + +#! + + *Exercise 1.6:* Alyssa P. Hacker doesn't see why `if' needs to be + provided as a special form. "Why can't I just define it as an + ordinary procedure in terms of `cond'?" she asks. Alyssa's friend + Eva Lu Ator claims this can indeed be done, and she defines a new + version of `if': + + (define (new-if predicate then-clause else-clause) + (cond (predicate then-clause) + (else else-clause))) + + Eva demonstrates the program for Alyssa: + + (new-if (= 2 3) 0 5) + 5 + + (new-if (= 1 1) 0 5) + 0 + + Delighted, Alyssa uses `new-if' to rewrite the square-root program: + + (define (sqrt-iter guess x) + (new-if (good-enough? guess x) + guess + (sqrt-iter (improve guess x) + x))) + + What happens when Alyssa attempts to use this to compute square + roots? Explain. + + +!#