From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Panicz Maciej Godek Newsgroups: gmane.lisp.guile.user Subject: Re: Define in let Date: Wed, 21 Aug 2013 12:17:43 +0200 Message-ID: References: <87k3jgb9kr.fsf@gnu.org> <20130820180137.301ace7e@capac> <20130821092851.GA16017@seid-online.de> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: multipart/alternative; boundary=047d7b6d8cc8f5bdb704e4727b68 X-Trace: ger.gmane.org 1377080274 1407 80.91.229.3 (21 Aug 2013 10:17:54 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Wed, 21 Aug 2013 10:17:54 +0000 (UTC) Cc: "guile-user@gnu.org" , Dmitry Bogatov , David Pirotte To: Ralf Mattes Original-X-From: guile-user-bounces+guile-user=m.gmane.org@gnu.org Wed Aug 21 12:17:56 2013 Return-path: Envelope-to: guile-user@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1VC5UC-0005qJ-IC for guile-user@m.gmane.org; Wed, 21 Aug 2013 12:17:56 +0200 Original-Received: from localhost ([::1]:52619 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VC5UC-00045Q-4T for guile-user@m.gmane.org; Wed, 21 Aug 2013 06:17:56 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:41210) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VC5U2-00043p-Fe for guile-user@gnu.org; Wed, 21 Aug 2013 06:17:47 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VC5U0-0002w1-IL for guile-user@gnu.org; Wed, 21 Aug 2013 06:17:46 -0400 Original-Received: from mail-ve0-x232.google.com ([2607:f8b0:400c:c01::232]:36733) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VC5U0-0002vr-Ci; Wed, 21 Aug 2013 06:17:44 -0400 Original-Received: by mail-ve0-f178.google.com with SMTP id ox1so153820veb.37 for ; Wed, 21 Aug 2013 03:17:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=0UAX0GhZg9nHdRuOWkxS80mbqlScsP8A67WuBntxJM4=; b=QTAl29s6+7mPzRn2RgzwD9QSFJZCsBj8aZD9dus59iF8Y58ivGZyeBnBiidytzmds3 b54QHTaxbH4NZknj7FSxvXseYRuJtYHEociQF6t1HL+1CvPr+Q8TAj83Q327KpCbvbSY TQjyG8hUftceXkxAONmfInrB1iewWUeSIeqRCHBotsXaJuw5WAyMvqo9BXZCxM4rs8QE n2OITNznPp0FXXN47Pe5Plg03/vEESgJruMekOz0GvKtbQQBnGGFa4kwU9wYKCPeZUIV DPEXePX4O15o+xI9p+5/DFlQv5BmhpiTYrw6wI9MxMH4o/K3C4jS7hosD3gWn6hI83QY SLNw== X-Received: by 10.58.196.132 with SMTP id im4mr317528vec.28.1377080263948; Wed, 21 Aug 2013 03:17:43 -0700 (PDT) Original-Received: by 10.221.45.135 with HTTP; Wed, 21 Aug 2013 03:17:43 -0700 (PDT) In-Reply-To: <20130821092851.GA16017@seid-online.de> X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2607:f8b0:400c:c01::232 X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-user-bounces+guile-user=m.gmane.org@gnu.org Original-Sender: guile-user-bounces+guile-user=m.gmane.org@gnu.org Xref: news.gmane.org gmane.lisp.guile.user:10660 Archived-At: --047d7b6d8cc8f5bdb704e4727b68 Content-Type: text/plain; charset=ISO-8859-1 2013/8/21 Ralf Mattes > On Wed, Aug 21, 2013 at 08:52:02AM +0200, Panicz Maciej Godek wrote: > > 2013/8/20 David Pirotte > > > > > 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 --047d7b6d8cc8f5bdb704e4727b68 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable



2013/8/21 Ralf Mattes <rm@seid-online.de>
On Wed, Aug 21, 2013 at 08:52:02AM +0200, Panicz Maciej G= odek wrote:
> 2013/8/20 David Pirotte <david@a= ltosw.be>
>
> > Hello,
> >
> > > It seems following is invalid:
> > >
> > > =A0 =A0(let ((a 2))
> > > =A0 =A0 =A0 =A0 (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))
> =A0 (set! foo (lambda (x) (+ a x))))

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

=A0 (define foo
=A0 =A0 =A0 =A0 =A0(let ((a 2))
=A0 =A0 =A0 =A0 =A0 =A0(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 a= ccessing
a single scope, you'd either need to use the solution with 'se= t!',
or -- as Taylan suggested -- have a "define-values&= quot; form.

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

(defi= ne-macro (define-values symbols . body)
=A0 (let ((value-identifi= ers (map gensym (map symbol->string symbols))))
=A0 =A0 `(begi= n
=A0 =A0 =A0 =A0,@(map (lambda(x)`(define ,x #f)) symbols)
= =A0 =A0 =A0 =A0(let-values ((,value-identifiers ,@body))
=A0 =A0 = =A0 =A0 =A0,@(map (lambda(symbol value)`(set! ,symbol ,value))
= =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 symbols
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 value-identifiers)))))

So we can say that guile already has that ;]
I do= n't know however how to write this using define-syntax, the
i= dea is to transform
(define-values (symbol1 symbol2 ...) body ...)
into
(begin
=A0 (define symbol1 #f)
=A0 (define symbol2 #f= )
=A0 ...
=A0 (let-values (((value1 value2 ...) (begin = body ...))
=A0 =A0 (set! symbol1 value1)
=A0 =A0 (set! symbol2 value2)<= /div>
=A0 =A0 ...)

but somehow we need to gen= erate identifiers for symbol1 symbol2 ...
(= here I wrote symbolically value1 value2 ..., but I'd appreciate if some= one
more competent could provide a syntax-rules-base= d solution)

regards
--047d7b6d8cc8f5bdb704e4727b68--