Newsgroups: comp.lang.scheme
Path: cantaloupe.srv.cs.cmu.edu!rochester!cornellcs!newsstand.cit.cornell.edu!news.kei.com!newsfeed.internetmci.com!in1.uu.net!demos!pluscom!usenet
From: moroz@inist.ru (Oleg Moroz)
Subject: Re: Help me to 'scheme-ify' this code
X-Newsreader: Forte Agent .99b.113
Sender: usenet@pluscom.cronyx.ru (Superuser)
Nntp-Posting-Host: maddie.inist.ru
Organization: Inist Ltd.
Message-ID: <DJvvw7.4o9@pluscom.cronyx.ru>
References: <4b769s$upe@intelsat1.intelsat.int>
Date: Wed, 20 Dec 1995 11:58:17 GMT
Lines: 57

On 19 Dec 1995 20:11:40 GMT, kimh@news.intelsat.int (Hong Kim) wrote:

> My question revolves around the 'accepted' use of let, let*, define,
> and set! in codes that are more or less straight line flow.  That is,
> even if you could code a recursive versionof my routine, I would
> choose the straight line method to match the reference.
> 
> You can see that I used defines and set!s everywhere, creating
> variables as I needed them.  It seems to me that let and let* are
> analogous to declaring your variables up front (as in C) but the
> syntax of let and let* admit assigment of values at 'declaration' time
> but variables like L2 and g2 aren't going to be assigned values until
> later in the code.
> 

[... actual code skipped ...]

Note that you can use (non-toplevel) defines only at the very start of
block (or implicit block). Note also that you can enclose let's in any
block to any depth, so the 'let' technique is much more flexible than
define's. Local define's are most useful for the definition of local
functions, especially recursive ones.

In fact, non-toplevel variable define's are usually implemented via
transformation to let's, e.g.

(begin 
  (define a 1) 
  (define b 2) 
  ...)

 => 

(let* 
  ((a 1) (b 2)) 
  ...)

Also, heavy usage of set!'s may preclude some compiler optimisations,
so that in many cases 

(begin 
  (define a XXX)
  ...
  (set! a YYY)
  ...)

is less efficient than 

(let ((a XXX))
  ...
  (let ((a YYY)) 
    ...))


Oleg


