guile-user
[Top][All Lists]
Advanced

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

Re: Define in let


From: Panicz Maciej Godek
Subject: Re: Define in let
Date: Wed, 21 Aug 2013 12:17:43 +0200




2013/8/21 Ralf Mattes <address@hidden>
On Wed, Aug 21, 2013 at 08:52:02AM +0200, Panicz Maciej Godek wrote:
> 2013/8/20 David Pirotte <address@hidden>
>
> > Hello,
> >
> > > It seems following is invalid:
> > >
> > >    (let ((a 2))
> > >         (define (foo x) (+ a x)))
> > >
> > > I prefer to reduce scope of variable as much as possible, so
> > > I find this restriction unconvinent. Is is part of standard or technical
> > > limitation? Is it any workaround?
> >
>
> The Scheme's idiomatic way to achieve the effect that you
> probably want would be
> (define foo #f)
> (let ((a 2))
>   (set! foo (lambda (x) (+ a x))))

I'd say this is extremly contorted and non-schemish.
What's wrong with:

  (define foo
         (let ((a 2))
           (lambda (arg) (+ a arg))))

This is the basic let-over-lambda closure ....


You're right, but it only works if you want to export only one symbol
from a lexical scope. If you wanted a few procedures accessing
a single scope, you'd either need to use the solution with 'set!',
or -- as Taylan suggested -- have a "define-values" form.

Actually, I think should also be possible to write a "define-values"
macro using the method I presented

(define-macro (define-values symbols . body)
  (let ((value-identifiers (map gensym (map symbol->string symbols))))
    `(begin
       ,@(map (lambda(x)`(define ,x #f)) symbols)
       (let-values ((,value-identifiers ,@body))
         ,@(map (lambda(symbol value)`(set! ,symbol ,value))
                symbols
                value-identifiers)))))

So we can say that guile already has that ;]
I don't know however how to write this using define-syntax, the
idea is to transform
(define-values (symbol1 symbol2 ...) body ...)
into
(begin
  (define symbol1 #f)
  (define symbol2 #f)
  ...
  (let-values (((value1 value2 ...) (begin body ...))
    (set! symbol1 value1)
    (set! symbol2 value2)
    ...)

but somehow we need to generate identifiers for symbol1 symbol2 ...
(here I wrote symbolically value1 value2 ..., but I'd appreciate if someone
more competent could provide a syntax-rules-based solution)

regards

reply via email to

[Prev in Thread] Current Thread [Next in Thread]