[plt-scheme] to define, or to let

From: Richard Cleis (rcleis at mac.com)
Date: Sat Mar 20 02:02:24 EST 2004

It seems that the bigger issue is that 'let forces the new variables 
into their own context.  I like that.

But how far should a schemer go?  For example, do schemers typically 
use 'letrec for a whole list if there is at least one dependency, or do 
they cascade 'let & 'letrec even though the program might be a bit 
messier?

(let ((strict-one 1)
       (strict-etc 2))
   (letrec ((fishy-one 1)
            (fishy-etc (+ 1 fishy-one)))
     #f))

rac


On Mar 19, 2004, at 10:14 PM, Mike T. Machenry wrote:

>   For list-related administrative tasks:
>   http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>
> Just to add my two-cents. I typically like to use let for values and 
> define
> for functions. This is because as mentioned earlier when I see let, I 
> know
> there's nothing fishy going on with the scope of the expressions in the
> bindings. However, I think parameters are easier to read when they're 
> in
> a define rather than a let.
>
> (lambda ()
>   (define (foo a b)
>     ...)
>   (foo ...))
>
> (lambda ()
>   (let ([foo
>          (lambda (a b)
>           ...)])
>     (foo ...)))
>
> Also there was a discussion a while back regarding the web-sever having
> problems with letrec bound page results. (Define works the same way 
> with
> respect to the example) This is probably beyond what you're asking for 
> but
> essentially the functions whose results were being defined had
> continuations which could be invoked multiple times later in the 
> program.
> Since define and letrec are destructive, the program had bug which 
> would
> not have existed had the values been bound in a let. So I guess I'm 
> saying
> the unforeseeable issues is a good reason to favor the most restrictive
> construct.
>
> -mike
>
> On Thu, Mar 18, 2004 at 10:31:01PM -0700, Richard Cleis wrote:
>>  For list-related administrative tasks:
>>  http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>>
>> Thanks.  That is why I try to use let-lists instead of
>> define-statements.  A compiler might also try to optimize
>> define-statements, but it seems that, in scheme, define-statements 
>> mean
>> something very specific.  That is what I am trying to figure out.
>>
>> BTW, using (time) and looping over the two functions shows that the 
>> let
>> form is about 10 percent faster than the define-form.  Ie, it looks
>> like the compiler *is* smart.
>>
>> rac
>>
>>
>> On Mar 18, 2004, at 9:58 PM, Zhu Chongkai wrote:
>>> 			
>>> Yes, a smart compiler will treat let without creating storage.
>>>
>>> For example:
>>>
>>> (let ((one-variable 3)
>>>      (another-variable 4))
>>>  (list one-variable another-variable))
>>>
>>> will first be converted to
>>>
>>> ((lambda (one-variable another-variable)
>>>   (list one-variable another-variable))
>>> 3 4)
>>>
>>> and then the code will be further optimized to
>>>
>>> (list 3 4)
>>>
>>>
>>> regards,
>>> Zhu Chongkai
>>



Posted on the users mailing list.