🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Introduction and Chapter 1

Started by
84 comments, last by bodchook 15 years, 7 months ago
Quote: Original post by SamLowry
Why the infinite computation?


Umm... maybe since it's a negative root the difference between guess and oldguess will be increasing, not decreasing? I'm not really sure... I'd have to play around with the code a little more than I've got time to do right now.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~I program in C++, on MSVC++ '05 Express, and on Windows. Most of my programs are also for windows.
Advertisement
Just finished exercise 1.3 (I'm going slowly I know):

(define (square x) (* x x))(define (sum-of-squares x y) (+ (square x) (square y)))(define (larger-sum-of-sqrs x y z)  (cond ((and (> x z) (> y z)) (sum-of-squares x y))        ((and (> x y) (> z y)) (sum-of-squares x z))        ((and (> y x) (> z x)) (sum-of-squares y z))))(larger-sum-of-sqrs (insert test numbers x y z))


The assignment didn't say anything about arguments of the same number. Did any of you guys include a test for equal numbers?
EDIT: I already found the bug in my code--my recursive version of cont-frac is broken. Fixed version appended to the message.

Exercise 1.38 is about approximating e using a continued fraction expansion of e - 2, where all the N terms are set to 1 and the D terms are a sequence as follows: 1, 2, 1, 1, 4, 1, 1, 6, 1, 1, 8, etc.

I wrote the following code, but it's not doing a very good job of approximating e:
(define (cont-frac n d k)  (if (= k 0)      0      (/ (n k) (+ (d k) (cont-frac n d (- k 1))))))(define (cont-frac-iter n d k)  (define (iter k result)    (if (= 0 k)        result        (iter (- k 1) (/ (n k) (+ result (d k))))))  (iter k 0))(define (calculate-e k)  (define (d i)    (let ((shifted-index (- i 2)))      (cond ((= i 2) 2.0)            ((= 0 (remainder shifted-index 3)) (+ 2.0 (* 2.0 (/ shifted-index 3.0))))            (else 1.0))))  (+ 2.0 (cont-frac (lambda (i) 1.0) d k)))


Running it with 10000 iterations:
> (calculate-e 10000)2.5000374981248825


Is something wrong with my code, or is the continued fraction approximation itself not good enough?

The problem was that my recursive cont-frac computed a different function (A continuous fraction that goes: Nk / (Dk + Nk-1 / (Dk-1 + ...)) instead of N1 / (D1 + N2 / (D2 + ...)). Fix0red:
(define (cont-frac n d k)  (define (inner-cont-frac i)  (if (= i k)      (/ (n i) (+ (d i)))      (/ (n i) (+ (d i) (inner-cont-frac (+ i 1))))))  (inner-cont-frac 1))


[Edited by - Muhammad Haggag on March 20, 2007 1:43:50 AM]

On the topic of exercise 1.5,

Is (define (p) (p)),

equivilant in Python to:

def badrecursion():
badrecursion()

?

Now that I've finished Chapter 1 (at laaaaaast!), there's one thing that's not entirely clear to me yet: "Visualization" (for lack of a better word) of higher-order procedures. Allow me to explain what I mean:

When I ran into exercise 1.41:
(define (double f)    (lambda (x) (f (f x))))(((double (double double)) inc) 5)

My immediate (and incorrect) thought was that it'd produce 13 (as in, increment by 8). It produced 21. I spent about 20 minutes mulling over why that's the result. Apparently, what I thought it'd do was actually this:
((double (double (double inc))) 5)


After a while, I was able to understand that the first form quadruples the quadruple of increment, hence incrementing 16 times. What I don't like is that I don't have a "consistent thought process" to arrive at that conclusion. I did it in an ad-hoc manner, and I'm afraid I won't be able to grok similar combinations for other, perhaps more complicated, higher order procedures.

So, my question rephrased would be: How do you go about deciphering such combinations, or visualize them in your head or on paper?

Quote: Original post by kevtimc
Just finished exercise 1.3 (I'm going slowly I know):

*** Source Snippet Removed ***

The assignment didn't say anything about arguments of the same number. Did any of you guys include a test for equal numbers?

Yes. I wrote it like this, with an else clause:
(define (square x) (* x x))(define (sum-of-squares x y)  (+ (square x) (square y)))(define (sum-of-squares-of-largest-two a b c)  (cond ((and (> a c) (> b c)) (sum-of-squares a b))        ((and (> a b) (> c b)) (sum-of-squares a c))        (else (sum-of-squares b c))))
Quote: Original post by kevtimc
On the topic of exercise 1.5,

Is (define (p) (p)),

equivilant in Python to:

def badrecursion():
badrecursion()

?
Yes.

Muhammad Haggag: I'll try to come up with a full answer for you, but maybe carefully following the single-step substitution rules and giving names to intermediate values would help?
Quote: Original post by Muhammad Haggag
So, my question rephrased would be: How do you go about deciphering such combinations, or visualize them in your head or on paper?


In order to better understand such constructions, I often invent my own notations and work with these; such syntactic abstractions often help me better understand complicated things.

I used the mathematical notation fn(x) to designate multiple application, e.g. f3(x) = f(f(f(3))). Thus, (double f) returns f2.

(double (double double)) inc
= (double double2) inc
= double4 inc
= inc24
= inc16

The doublen f = f2n relation can easily be found and proven by induction.

I don't know if this helps you, but I was able to decipher it quite easily this way.

Also, I consider this example a fairly complex one; most other uses of higher-order functions are far more simple than this IMHO, so understanding other uses won't be as hard.
Quote: Original post by Rebooted
Muhammad Haggag: I'll try to come up with a full answer for you, but maybe carefully following the single-step substitution rules and giving names to intermediate values would help?

Indeed, thank you very much! [smile]

I had tried the following single-step substitution, but I always got stuck at the highlighted step, having substituted the outer double with its definition (without using an intermediate value). That is:
(((double (double double)) inc) 5)(((double ((lambda (x) (f (f x))) double)) inc) 5)(((double (lambda (x) (double (double x)))) inc) 5) <-- I was getting stuck after this, when expanding the outer double

Using an intermediate value does simplify things immensely:
Let g = ((lambda (x) (double (double x))))(((double g) inc) 5)(((lambda (x) (g (g x))) inc) 5)((g (g inc)) 5)((g (double (double inc))) 5)((g (double inc2)) 5)((g inc4) 5)((double (double inc4)) 5)((double inc8) 5)(inc16 5)21

Rawrrrrrr!!

Quote: In order to better understand such constructions, I often invent my own notations and work with these; such syntactic abstractions often help me better understand complicated things.

I used the mathematical notation fn(x) to designate multiple application, e.g. f3(x) = f(f(f(3))). Thus, (double f) returns f2.

...

I don't know if this helps you, but I was able to decipher it quite easily this way.

Yes, this makes it pretty easy to wrap my mind around--the invented notation is very helfpul.

Quote: Also, I consider this example a fairly complex one; most other uses of higher-order functions are far more simple than this IMHO, so understanding other uses won't be as hard.

Good to know [smile]

Hi All!

I am currently trying to do exercise 1.22.

I am using DrScheme version 360 on a windows XP machine.

I am getting a runtime error


error start here ==>
reference to undefined identifier: runtime
<==error ends here

I was wondering if anyone knows what runtime procedure is called under this implementation of lisp?

Thanks in advance!

This topic is closed to new replies.

Advertisement