Introduction and Chapter 1

Started by
84 comments, last by bodchook 15 years, 4 months ago
Quote:Original post by Ceoddyn
Copying from my last post as it was poorly placed in my post

I can see how people say lisp is a fun language to use, its a change from the norm, but I was wondering if anyone has some examples of applications/games created in lisp?


Applications made in Common Lisp. Emacs uses Emacs Lisp, I believe The Gimp uses a Lisp variant too, as does Autodesk's AutoCAD. I've heard the .NET Garbage Collector was written in Lisp. Jak and Daxter also used a Lisp variant internally.
Advertisement
Quote:Original post by SamLowry
Quote:Original post by Ceoddyn
Copying from my last post as it was poorly placed in my post

I can see how people say lisp is a fun language to use, its a change from the norm, but I was wondering if anyone has some examples of applications/games created in lisp?


Applications made in Common Lisp. Emacs uses Emacs Lisp, I believe The Gimp uses a Lisp variant too, as does Autodesk's AutoCAD. I've heard the .NET Garbage Collector was written in Lisp. Jak and Daxter also used a Lisp variant internally.


thanks for the response, nice to see some actual work being done with lisp
Please refrain from posting off-topic questions in this thread. We have a full forum dedicated to SICP and Scheme, so please use it [smile]

I'm on Exercise 1.29 right now. It's for approximating integrals with Simpson's rule. However, I haven't learned about integrals in math yet, so I'd like an alternative problem to this, if possible, that's less mathy. [smile] Thanks in advanced.

Also, could someone verify for me that this is computed iteratively(Excercise 1.30)?

(define (sum a b term next)  (define (iter a total)    (if (> a b)        total        (iter (next a) (+ (term a) total))))  (iter a 0))


Edit: Cleaned up the code a bit.
Quote:Original post by Ezbez
Also, could someone verify for me that this is computed iteratively(Excercise 1.30)?

*** Source Snippet Removed ***

Edit: Cleaned up the code a bit.


Looks correct to me (except perhaps the order of the arguments if you really want to be pedantic).
Quote:Original post by Ezbez
I'm on Exercise 1.29 right now. It's for approximating integrals with Simpson's rule. However, I haven't learned about integrals in math yet, so I'd like an alternative problem to this, if possible, that's less mathy. [smile] Thanks in advanced.

You don't really have to know about integrals to be able to solve this exercise. I'll try to help you solving this problem: first of all, don't get discouraged by the ugly formula :)

We first handle the factor before it: we compute it, and store the result in a variable, like this:
(define (simpson f a b n)  (define h [expression for h])  ...)


Next, we take on the big sum, which we know we'll have to compute as
(sum [term] [a] [next] )

The values for 'a', 'b' and 'next' are easy: there are (n+1) terms, numbered from 0 till n. 'term' is the harder part.

Each term has its own y_k value, so we can define a function for that:
(define (simpson f a b n)  (define h [expression for h])  (define (y k) ...)  (sum [term] [a] [next] ))

So, we still need 'term': the coefficients (i.e. the constants before each y_k) are: 1 4 2 4 2 4 ... 4 2 4 2 1, meaning for k = 0 and k = n, the coefficient equals 1, and in between they alternate between 4 and 2. We put this in a separate function:
(define (simpson f a b n)  (define h [expression for h])  (define (y k) ...)  (define (term k)    (* (y k)       (cond ((or (= k 0)                  (= k n)) 1)             ((= (modulo k 2) 1) 4)             (else 2))))  (sum term [a] [next] ))

And we're done. So, this leads to the solution:
(define (simpson f a b n)  (define h (/ (- b a)               n))  (define (y k)    (f (+ a          (* k h))))  (define (term k)    (* (cond ((or (= k 0)                  (= k n)) 1)             ((= (modulo k 2) 1) 4)             (else 2))       (y k)))  (define (1+ x) (+ 1 x))  (* (/ h 3)     (sum term 0 1+ n)))


An alternative exercise (I see 1.29 as an exercise on using sum) which I remember from high school: you build a tower by stacking an infinite amount of cubes, whose sizes are 1, 1/2, 1/3, 1/4, 1/5, 1/6, ...
Three questions:
* is it possible to build another tower of finite height which fully contains the cube-tower?
* is it possible to paint the cube-tower with an infinite finite amount of paint? (all 6 sides of the cubes are to be painted completly)
* is it possible to have a bag of finite size which contains all the cubes (but they don't need to be stacked as in the first question, so the difference with the first question is that here you compute the total volume, not the total height)

Guess first, and then compute the three sums. The goal at school was to prove our answers formally, but for me you can just compute the sums and see if there seems to be an upper limit or not (e.g. try a tower of height 10, then 100, then 1000, ... and see if the result keeps getting larger (going to infinity) or hits a limit).


And another one (this one is more like 1.30): define me a function taking three arguments 'init-amount', 'rate' and 'goal' and computes me the number of years it takes for 'init-amount' to grow to 'goal' if every year I get intrest at rate 'rate'.
E.g. if 'init-amount' is 100, 'rate' is 10, and 'goal' is 200, it will compute:
year 0: 100
year 1: 100 + 10% = 110
year 2: 110 + 10% = 121
year 3: 121 + 10% = 133
year 4: 133 + 10% = 146
year 5: 146 + 10% = 161
year 6: 161 + 10% = 177
year 7: 177 + 10% = 194
year 8: 194 + 10% = 214
so the function should return 8. It's possible to do this with a recursive process and an iterative process. Try both.

[Edited by - SamLowry on November 29, 2006 4:51:48 AM]
Nice posts and thanks for the reply! It'll take me a bit to work through it all, but I'll give you my answers when I'm done.
Quote:Original post by SamLowry
An alternative exercise (I see 1.29 as an exercise on using sum) which I remember from high school: you build a tower by stacking an infinite amount of cubes, whose sizes are 1, 1/2, 1/3, 1/4, 1/5, 1/6, ...
Three questions:
* is it possible to build another tower of finite height which fully contains the cube-tower?
* is it possible to paint the cube-tower with an infinite amount of paint? (all 6 sides of the cubes are to be painted completly)
* is it possible to have a bag of finite size which contains all the cubes (but they don't need to be stacked as in the first question, so the difference with the first question is that here you compute the total volume, not the total height)

Guess first, and then compute the three sums. The goal at school was to prove our answers formally, but for me you can just compute the sums and see if there seems to be an upper limit or not (e.g. try a tower of height 10, then 100, then 1000, ... and see if the result keeps getting larger (going to infinity) or hits a limit).ble to do this with a recursive process and an iterative process. Try both.


Well, I've been trying this problem lately. While looking for more information about this, I noticed that my Calculus course actually teaches convergence/divergence after integrals. Bleck. I could still do as you said, just to go higher and higher, though. My problem became that any more than 10,000 boxes ran extremely slowly (10,000 took half a minute itself), and
it didn't seem to have been reaching an upper limit yet (doing 10 times more numbers added roughly 2 to the result and looked like it would take a very long time for it to stop adding much). Well, there you have it, the height of the tower doesn't approach an upper limit, at least not up to 10,000. My guess was that it would level off.

Here's the code that I used (note: remember my wacky order of the parameters):
(define (inverse a)  (/ 1 a))(define (add-one a)  (+ a 1))(exact->inexact (sum 1 10000 inverse add-one))

sum was iterative (the one that I showed you last page).

The next question confused me. How could you *not* be able to paint something if you have an infinite amount of paint?
Quote:Original post by Ezbez
Well, I've been trying this problem lately. While looking for more information about this, I noticed that my Calculus course actually teaches convergence/divergence after integrals. Bleck. I could still do as you said, just to go higher and higher, though. My problem became that any more than 10,000 boxes ran extremely slowly (10,000 took half a minute itself), and
it didn't seem to have been reaching an upper limit yet (doing 10 times more numbers added roughly 2 to the result and looked like it would take a very long time for it to stop adding much). Well, there you have it, the height of the tower doesn't approach an upper limit, at least not up to 10,000. My guess was that it would level off.

My pc (P4 2GHz) is able to go to 1,000,000 in 1.492 seconds (there's the function 'time' which you could use to measure how long it takes to evaluate an expression: (time (fib 1000))). Assuming you don't work on a 10 year old computer, think about what could cause your calculation to go so slowly, and which simple trick I used to make the computation run a lot faster.
Small hint: I didn't modify the 'sum'-code, nor did I apply some fancy calculus/algebra/... trick.
Bigger reversed hint: melborp eht sevlos woleb edoc ruoy ot retcarahc eno gniddA

Also, your conclusion is correct: there's no upper limit for the first problem, the tower gets infinitely high (but it does build up very slowly: it takes 10440 boxes to get to height 1000, and no, I didn't use scheme to compute that :)).

Quote:
Here's the code that I used (note: remember my wacky order of the parameters):
(define (inverse a)  (/ 1 a))(define (add-one a)  (+ a 1))(exact->inexact (sum 1 10000 inverse add-one))

sum was iterative (the one that I showed you last page).

That's fully correct.

Quote:
The next question confused me. How could you *not* be able to paint something if you have an infinite amount of paint?

Sorry, my mistake, had to be "finite amount", I've updated it.

[Edited by - SamLowry on November 29, 2006 5:14:10 AM]
Dunno if this is what you were talking about, but I'm able to get similar performance to you (766 ms for 1,000,000 on a 3ghz P4) now that I'm computing (sum 1.0 1000000.0 inverse add-one). I guess that rational-number math is considerably slower. Judging by your clue, this is what you're talking about, though it does involve two characters.

I'll work on the paint problem now.

Edit: I get that you could paint them all with a finite amount of paint. It seems to level off at about 10.

I just used this function instead of inverse:
(define (area-of-cube a)  (* (inverse a) (inverse a) 6))

This topic is closed to new replies.

Advertisement