On Jan 23, 2008 7:16 PM, Mark Schoonover <[EMAIL PROTECTED]> wrote:
> Ex. 1.1
*this one was pretty simple, I'll skip it
> Ex. 1.2
>
> Welcome to DrScheme, version 372 [3m].
> Language: Advanced Student.
>
> > (+ 5 4 (- 2 (- 3 (+ 6 (/ 4 5)))))
> 14.8
Why not 74/5?
> > (* 3 (- 6 2) (- 2 7))
> -60
> > (/ (+ 5 4 (- 2 (- 3 (+ 6 (/ 4 5)))))
> (* 3 (- 6 2) (- 2 7)))
> -0.246
Why not -37/150?
> Ex. 1.3
>
> (define (square x) (* x x))
> (define (sos x y) (+ (square x) (square y)))
> (define (big3-sos x y z)
> (cond ((and (< x y) (< y z)) (sos y z))
> ((and (< z x) (< z y)) (sos x y))
> (else (sos x z))))
I did this slightly differently. Your big3-sos definition falls down
when two of the arguments are equal (in that case, it always returns
the value of the "else" option, summing squares of x and z). I just
used the operator '<=' each time you used '<'.
> Ex. 1.4
>
> If b is less than zero, the - procedure is called to subtract a from
> -b. If b is greater than zero, the + procedure is called to simply add
> a and b.
Actually, if b<0, it subtracts *b* from *a* (- a b). Then, since in
this case b is negative, subtracting the negative value is the same as
adding its absolute value.
> Ex. 1.5
>
> I would think applicative order would return zero since it's going to
> evaluate x first. Normal order, I'm not sure what it'll do. Trying it
> with drscheme, it just keeps running forever without any end. Consumes
> about 50% CPU, and eventually kicks the fan on the laptop CPU. When I
> stop the process, it always stoped on (p) in the define.
Yes, normal order attempts to evaluate the operator and all operands
to primitive values before performing any sort of substitution. Since
(p) always evaluates back to (p) (a non-primitive), that is why it
will hang.
...and there are more exercises later on in the chapter, after 1.1.7
of the text!
Ex. 1.6
'new-if' uses cond to spawn a sub-environment (or "child process,"
maybe?) for each recursion of sqrt-iter, and it never comes back out
to deliver a value to the function. On the other hand, the special
form 'if' stays at the top level of the definition and can return a
value to the function.
Ex. 1.7
When computing the square root of a very small number, 0.001 is not a
good enough threshold because it's likely a very large proportion of
the final answer (what if you're computing (sqrt 0.001)?). When a very
large number is the argument of sqrt, each iteration of the guess is
extremely variable and a lot of tme will be wasted with that much
precision. Therefore, here is a good replacement for 'good-enough?':
(define (good-enough? guess x)
(if (or (< (/ (square guess) x) 1.001)
(> (/ (square guess) x) .999)) #t
#f))
In English, if (the guess squared) divided by the argument is between
.999 and 1.001, return true. Otherwise return false. Now, whether the
argument is small or large, 'good-enough?' compares the guess and
argument proportionally instead of having a fixed threshold.
Ex. 1.8
To use the cube-root function, only 'improve' and 'good-enough?' need
to be changed:
(define (improve guess x)
(/ (+ (/ x (square guess)) (* 2 guess)) 3))
And, in my improved 'good-enough?' above, just put in (cube guess)
each place you see (square guess), and add (define (cube x) (* x x x))
elsewhere.
--
Brad Beyenhof http://augmentedfourth.com
The history of popular music is littered with great partnerships.
Rodgers had his Hammerstein, Lennon had his McCartney, and Lloyd Webber
had... his photocopier... ~Humphrey Lyttleton
--
[email protected]
http://www.kernel-panic.org/cgi-bin/mailman/listinfo/kplug-lpsg