[BULK] Re: [plt-scheme] Re: (newbie question) About Loop

From: Stephen Bloch (sbloch at adelphi.edu)
Date: Fri Jun 12 07:50:26 EDT 2009

On Jun 12, 2009, at 7:21, emre berat nebioğlu <beratn at gmail.com> wrote:

> Can we use while or do while in drscheme.Just i wonder.I mean that
> While(list != null)
>      list=list.cdr

There are ways to do that, but if you start with "I want to write a  
for-loop or a while-loop; what is the Scheme syntax for it?", you  
won't end up with a Scheme program, but rather a C program in Scheme  
syntax.  Sort of like putting a passage of Shakespeare through  
Babelfish and translating each individual word into Russian.

To get used to the Scheme "way", pretend for a moment there were no  
assignment statement in your favorite language (Java, C++, whatever):  
you can declare a variable with an initial value, but you can't change  
the value of an existing variable.  Loops wouldn't work any more,  
because they rely on some variable taking on a different value each  
time through the loop... but conditionals and function calls would  
work just fine, so the natural analogue to the loops you're used to  
would be recursion.  After a few days or weeks of programming that  
way, recursion would come easily to you, and you would wonder why  
anybody bothers with while-loops :-)

A little later, you might get frustrated at writing the same recursive  
structure over and over, and you would learn about higher-order  
functions and comprehensions, which are far more powerful and flexible  
than while-loops, and you would start to pity people who are forced to  
live in a world of while-loops.

Scheme DOES have assignment, but I don't teach it to my beginning  
programming students until they've seen these other techniques.

Steve Bloch

Posted on the users mailing list.