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 08:52:02 +0200 Message-ID: References: <87k3jgb9kr.fsf@gnu.org> <20130820180137.301ace7e@capac> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: multipart/alternative; boundary=001a11c3bea057765004e46f9c23 X-Trace: ger.gmane.org 1377077365 1769 80.91.229.3 (21 Aug 2013 09:29:25 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Wed, 21 Aug 2013 09:29:25 +0000 (UTC) Cc: "guile-user@gnu.org" , Dmitry Bogatov To: David Pirotte Original-X-From: guile-user-bounces+guile-user=m.gmane.org@gnu.org Wed Aug 21 11:29:27 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 1VC4jH-0006es-Fi for guile-user@m.gmane.org; Wed, 21 Aug 2013 11:29:27 +0200 Original-Received: from localhost ([::1]:52368 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VC4jH-000202-2A for guile-user@m.gmane.org; Wed, 21 Aug 2013 05:29:27 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:55253) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VC4iz-0001t2-UP for guile-user@gnu.org; Wed, 21 Aug 2013 05:29:12 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VC2Gx-0007X5-F8 for guile-user@gnu.org; Wed, 21 Aug 2013 02:53:27 -0400 Original-Received: from mail-vb0-x22c.google.com ([2607:f8b0:400c:c02::22c]:43881) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VC2Gx-0007Wt-5k; Wed, 21 Aug 2013 02:52:03 -0400 Original-Received: by mail-vb0-f44.google.com with SMTP id e13so22039vbg.31 for ; Tue, 20 Aug 2013 23:52:02 -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=VU0DHafIe+JOjtNcOyw2EVroU9sLaoPVxwX0iytj1P0=; b=ns3vexlw1PYmsdwQg2mc6SfPOnKtLBjJLX2msnRiSpeTADHRYJ9ZjVI2/VJbkMQjZA j4gEuZuCF8Z6R7Sv+IHA6epuOLGhr15OXy60tj+PLd+kcBD7uxFwUuM21Q8ZtPEaaAR2 FOnOhKi5+QBtT1CU8IvRcxY+nv6AS4EZby9hop+Y/1mbdSgRbHcd4Cj6jCnvfqkePVbG PVV5IUCA58eAEsMiNb1NwIPH79nbD0kRdHgeJePqbe/ypcQuHCvMbonR6S7lzXQsvBC5 aBT7vYBBkzO0J6VUuJvr5TkgfAKJtEJwubWW47psbty5r/FY/2b4jU7efTTWc7F3aHb3 ZT1Q== X-Received: by 10.220.16.73 with SMTP id n9mr522083vca.24.1377067922321; Tue, 20 Aug 2013 23:52:02 -0700 (PDT) Original-Received: by 10.221.45.135 with HTTP; Tue, 20 Aug 2013 23:52:02 -0700 (PDT) In-Reply-To: <20130820180137.301ace7e@capac> X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2607:f8b0:400c:c02::22c 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:10658 Archived-At: --001a11c3bea057765004e46f9c23 Content-Type: text/plain; charset=ISO-8859-1 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)))) Although it allows to achieve the desired effect, it doesn't express the programmer's intention quite clearly. If you're interested, I recently wrote a macro that would allow to get the same in a IMHO slightly more elegant way, i.e. (publish (define (foo x) (+ a x)) where (define a 2)) The macro's definition (procedural) follows. Regards. ============================= (define-macro (publish . definitions) (define (interface-name interface) (match interface ((head . tail) (interface-name head)) ((? symbol? name) name))) (let-values (((public-definitions where&private-definitions) (split-before (equals? 'where) definitions))) `(begin ,@(map (match-lambda ((define-variant interface . body) `(define ,(interface-name interface) #f))) public-definitions) (let () ,@(match where&private-definitions (('where . private-definitions) private-definitions) (() '())) ,@(map (match-lambda ((define-variant interface . body) (let ((name (interface-name interface))) `(set! ,name (let () (,define-variant ,interface . ,body) ,name))))) public-definitions))))) --001a11c3bea057765004e46f9c23 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable

= 2013/8/20 David Pirotte <david@altosw.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 technic= al
> limitation? Is it any workaround?

The Scheme's idiomatic way to achieve the effect that you=A0
probably want would be
(define foo #f)
(let ((a 2= ))
=A0 (set! foo (lambda (x) (+ a x))))

Although= it allows to achieve the desired effect, it doesn't
express = the programmer's intention quite clearly.
If you're inter= ested, I recently wrote a macro that would
allow to get the same in a IMHO slightly more elegant way, i.e.
<= div>
(publish
=A0 (define (foo x) (+ a x))
=A0where
=A0 (define a 2))
=A0
The macro= 9;s definition (procedural) follows.
Regards.

=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
(define-= macro (publish . definitions)
=A0 (define (interface-name interfa= ce)
=A0 =A0 (match interface
=A0 =A0 =A0 ((head . tail)=
=A0 =A0 =A0 =A0(interface-name head))
=A0 =A0 =A0 ((? symbol= ? name)
=A0 =A0 =A0 =A0name)))
=A0 (let-values (((publi= c-definitions where&private-definitions)
=A0 =A0 =A0 =A0 =A0 = =A0 =A0 =A0 (split-before (equals? 'where) definitions)))
=A0 =A0 `(begin ,@(map (match-lambda
=A0 =A0 =A0 =A0 =A0 =A0= =A0 =A0 =A0 =A0 =A0 =A0((define-variant interface . body)
=A0 = =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 `(define ,(interface-name inter= face) #f)))
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0public-definit= ions)
=A0 =A0 =A0 =A0 =A0 =A0 (let ()
=A0 =A0 =A0 =A0 =A0 =A0 =A0 = ,@(match where&private-definitions
=A0 =A0 =A0 =A0 =A0 =A0 = =A0 =A0 =A0 (('where . private-definitions)
=A0 =A0 =A0 =A0 = =A0 =A0 =A0 =A0 =A0 =A0private-definitions)
=A0 =A0 =A0 =A0 =A0 = =A0 =A0 =A0 =A0 (() '()))
=A0 =A0 =A0 =A0 =A0 =A0 =A0 ,@(map (match-lambda
=A0 =A0 =A0= =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0((define-variant interface . body)<= /div>
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 (let ((name (= interface-name interface)))
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0 =A0 =A0 =A0 =A0 `(set! ,name
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0(let ()
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0 =A0 =A0 =A0 =A0 =A0(,define-variant ,interface . ,body)
=A0 = =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0,nam= e)))))
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0public-definiti= ons)))))

--001a11c3bea057765004e46f9c23--