Skip to content

Commit 1fb46e6

Browse files
authored
Merge pull request #2 from rmrgh/master
docs: enunciado dos exercícios 1.1 ao 1.8
2 parents 046d4ca + f7a49d6 commit 1fb46e6

File tree

8 files changed

+86
-0
lines changed

8 files changed

+86
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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.
2+
3+
```scheme
4+
10
5+
(+ 5 3 4)
6+
(- 9 1)
7+
(/ 6 2)
8+
(+ (* 2 4) (- 4 6))
9+
(define a 3)
10+
(define b (+ a 1))
11+
(+ a b (* a b))
12+
(= a b)
13+
(if (and (> b a) (< b (* a b)))
14+
b
15+
a)
16+
(cond ((= a 4) 6)
17+
((= b 4) (+ 6 7 a))
18+
(else 25))
19+
(+ 2 (if (> b a) b a))
20+
(* (cond ((> a b) a)
21+
((< a b) b)
22+
(else -1))
23+
(+ a 1))
24+
```
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Translate the following expression into prefix form.
2+
3+
![](https://latex.codecogs.com/gif.latex?%5Cfrac%7B5&plus;4&plus;%282-%283-%286%20&plus;%5Cfrac%7B5%7D%7B4%7D%29%29%29%7D%7B3%286-2%29%282-7%29%7D)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Define a procedure that takes three numbers as arguments and returns the sum of the squares of the two larger numbers.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
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:
2+
3+
```scheme
4+
(define (a-plus-abs-b a b)
5+
((if (> b 0) + -) a b))
6+
```
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
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:
2+
3+
```scheme
4+
(define (p) (p))
5+
6+
(define (test x y)
7+
(if (= x 0)
8+
0
9+
y))
10+
```
11+
12+
Then he evaluates the expression
13+
14+
```scheme
15+
(test 0 (p))
16+
```
17+
18+
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.)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
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`:
2+
3+
```scheme
4+
(define (new-if predicate then-clause else-clause)
5+
(cond (predicate then-clause)
6+
(else else-clause)))
7+
```
8+
9+
Eva demonstrates the program for Alyssa:
10+
11+
```scheme
12+
(new-if (= 2 3) 0 5)
13+
5
14+
15+
(new-if (= 1 1) 0 5)
16+
0
17+
```
18+
19+
Delighted, Alyssa uses `new-if` to rewrite the square-root program:
20+
21+
```scheme
22+
(define (sqrt-iter guess x)
23+
(new-if (good-enough? guess x)
24+
guess
25+
(sqrt-iter (improve guess x)
26+
x)))
27+
```
28+
What happens when Alyssa attempts to use this to compute square roots? Explain.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The `good-enough?` test used in computing square roots will not be very effective for finding the square roots of very small numbers. Also, in real computers, arithmetic operations are almost always performed with limited precision. This makes our test inadequate for very large numbers. Explain these statements, with examples showing how the test fails for small and large numbers. An alternative strategy for implementing `good-enough?` is to watch how `guess` changes from one iteration to the next and to stop when the change is a very small fraction of the guess. Design a square-root procedure that uses this kind of end test. Does this work better for small and large numbers?
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Newton's method for cube roots is based on the fact that if y is an approximation to the cube root of x, then a better approximation is given by the value
2+
3+
![](https://latex.codecogs.com/gif.latex?%5Cfrac%7Bx/y%5E2&plus;2y%7D%7B3%7D)
4+
5+
Use this formula to implement a cube-root procedure analogous to the square-root procedure. (In section [1.3.4](https://mitpress.mit.edu/sicp/full-text/book/book-Z-H-12.html#%_sec_1.3.4) we will see how to implement Newton's method in general as an abstraction of these square-root and cube-root procedures.)

0 commit comments

Comments
 (0)