[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: peasant revolt against DrScheme!



Michael,

> Factorial, no doubt.  

Thanks.  You, and the others who have replied, have all nicely slipped
into the trap.  It's interesting, too, how the replies I've received
don't just say "factorial"; they always add a note of absolutely
certainty to it.

First, an incindiary remark (just in case you weren't paying
attention): Factorial is the worst first example of recursion.  ...
Except for fibonacci, which is even worse.

Now: go see how How to Design Programs does it.  If you read it
carefully enough, you will find that

(a) we are sympathetic to the student's question about why they
    shouldn't just use a loop, and

(b) develop a response to it.

Indeed, by the fifth week of the course at Rice, these questions are
definitively quashed.

(You anticipate the response in your message; for some reason, you
don't make the connection yourself.)

> Many students are probably never even taught that there are more
> conventional iteration constructs in scheme e.g.
> 
> (define (factorial n)
>    (define r 1)
>    (do ((i 2 (+ i 1)))
>        ((> i n) r)
> 	   (set! r (* r i))))

And thank goodness for that.  Just yesterday I saw a VB teacher
complaining on the AP computer science mailing list that he simply
couldn't figure out the syntax of Scheme's conditionals.  If he were
only to see the syntax of DO ...

[I've tried using DO 5-10 times in my life.  Every single time I get
 the syntax horribly wrong and give up.]

> Recursion is important when writing functions to handle recursive data
> structures (e.g. trees).  It also sometimes, but not always, leads to
> cleaner and easier-to-understand programs.  It also typically leads to
> programs that have fewer side-effects, are more functionally pure,
> etc. etc.  However, very often ordinary iteration is just the most
> straightforward way to go, and since scheme supports that as well, why not
> take advantage of it?  

Because, as you observe, if you simplify the looping construct enough, 
you are forced to use mutation.  And it is obvious (I hope) that
beginning students should not be taught mutation.

Shriram