# [plt-scheme] trying to figure out the do form

 From: ifconfig nslookup (configurator at gmail.com) Date: Mon Oct 11 19:56:47 EDT 2004 Previous message: [plt-scheme] trying to figure out the do form Next message: [plt-scheme] trying to figure out the do form Messages sorted by: [date] [thread] [subject] [author]

```I think I can clarify your problem.
Let's trace the results of (factorial 4) using your function's output.
With the first form, these are the results you get. And they are indeed correct.
Note, that oI is the value of I from the previous step, and oA is the
previous value of A.

Step 1.   (i, a 4 ,  1)   I = 4, A = 1.
Step 2.   (i, a 3 ,  4)   I = oI - 1 = 4 - 1 = 3, A = oA * oI = 1 * 4 = 4.
Step 3.   (i, a 2 ,  12)   I = oI - 1 = 3 - 1 = 2, A = oA * oI = 4 * 3 = 12.
Step 4.   (i, a 1 ,  24)   I = oI - 1 = 2 - 1 = 1, A = oA * oI = 12 * 2 = 24.
And step 5.   ans is  (i, a 0 , 24)   I = oI - 1 = 1 - 1 = 0, A = oA *
oI = 24 * 1 = 0.
With this form, A was always multiplied *before* I was decremented.

The second form:
Step 1.   (i, a 4 ,  1)   I = 4, A = 1.
Step 2.   (i, a 3 ,  3)   I = oI - 1 = 4 - 1 = 3, A = oA * I = 1 * 3 =
3;   note that A is multiplied after I is decremented.
Step 3.   (i, a 2 ,  6)   I = oI - 1 = 3 - 1 = 2, A = oA * I = 3 * 2 = 6.
Step 4.   (i, a 1 ,  6)   I = oI - 1 = 2 - 1 = 1, A = oA * I = 6 * 1 = 6.
And step 5.   ans is  (i, a 0 , 0)   I = oI - 1 = 1 - 1 = 0, A = oA *
I = 6 * 0 = 0.

Here, A is multiplied after I is decremented, which causes it to be 3
* 2 * 1 * 0 = 0, instead of 4 * 3 * 2 * 1 = 24.

Yours,
ifconfig.

On Mon, 11 Oct 2004 15:38:17 -0700, Sanjeev Sharma <throwit1 at hotmail.com> wrote:
>  http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>
> I thought these 2 would be equivalent, and the description seems to say they
> should be.
>
> My self-appointed exercise was to move the update clauses around, so the (-
> i 1) was deleted from the (var val update) line & put into the do-if-false
> expression list.
>
> Can someone show me where I'm messing up?
>
> (define factorial
>    (lambda (n)
>    (do ((i n (- i 1)) (a 1 (* a i)))
>        ((zero? i)(display "ans is  ")(display `("i, a",i",",a))(newline))
>      (display `("i, a",i", ",a))(newline))))
> (factorial 4)
> (define factorial
>  (lambda (n)
>    (do ((i n ) (a 1 (* a i)))
>      ((zero? i) (display "ans is  ")(display `("i, a",i",",a))(newline))
>      (display `("i, a",i", ",a))(newline)(set! i(- i 1)))))
> (factorial 4)
>
> _________________________________________________________________
> Express yourself instantly with MSN Messenger! Download today - it's FREE!
> http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/
>
>

```

 Posted on the users mailing list. Previous message: [plt-scheme] trying to figure out the do form Next message: [plt-scheme] trying to figure out the do form Messages sorted by: [date] [thread] [subject] [author]