Newsgroups: comp.lang.scheme
Path: cantaloupe.srv.cs.cmu.edu!nntp.club.cc.cmu.edu!godot.cc.duq.edu!news.duke.edu!MathWorks.Com!udel!princeton!nimaster.princeton.edu!blume
From: blume@beth.cs.princeton.edu (Matthias Blume)
Subject: Re: Tcl/Lisp/Python: A "User" point of view
In-Reply-To: net@cs.tu-berlin.de's message of 1 Oct 1994 12:37:19 GMT
Message-ID: <BLUME.94Oct1153023@beth.cs.princeton.edu>
Originator: news@nimaster
Sender: news@Princeton.EDU (USENET News System)
Nntp-Posting-Host: beth.cs.princeton.edu
Organization: Princeton University
References: <9409232314.AA29957@mole.gnu.ai.mit.edu> <Cww0DA.I68@cwi.nl>
	<BLUME.94Sep29094838@beth.cs.princeton.edu>
	<36hn4v$ak6@csnews.cs.Colorado.EDU> <36jl5v$6dc@news.cs.tu-berlin.de>
Date: Sat, 1 Oct 1994 19:30:23 GMT
Lines: 50

In article <36jl5v$6dc@news.cs.tu-berlin.de> net@cs.tu-berlin.de (Oliver Laumann) writes:

   Also note that the comparison is slightly unfair, as the do-loop is one
   of the more baroque constructs of Scheme and does not fit well into the
   `spirit' of the language (as opposed, for instance, to the `named let'
   loop construct or a simple tail-recursive procedure which I would have
   used instead).  This is not the case with Perl's for-loop, if I'm
   not mistaken.

Ok, since you asked for it:

using `do':

(do ((i 0 (+ i 1))
     (l '() (cons i l)))
    ((>= i 10000) l))

using a `named let':

(let loop
    ((i 0)
     (l '()))
  (if (>= i 10000)
      l
      (loop (+ i 1) (cons i l))))

as a tail-recursive procedure:

(letrec ((loop (lambda (i l)
                 (if (>= i 10000)
                     l
                     (loop (+ i 1) (cons i l))))))
  (loop 0 '()))

My guess is that to the untrained eye the first (`do'-based) version
is the most compact one.  So I *was* unfair -- but admittedly it was
in *favor* of Scheme.

Note, that I use `do' rarely in my real programs because most of the
time it doesn't fit my needs.

At any rate, all three of the above versions are *exactly* equivalent
anyway.  Many implementations expand both the `do' and the `named let'
version into the `explicit tail recursion' form before the compiler
ever gets a chance to see it.  (Actually, using `do' one doesn't need
to invent a name for the loop, the macro expander does this for
you...)

--
-Matthias
