From: Panicz Maciej Godek <godek.maciek@gmail.com>
To: David Pirotte <david@altosw.be>
Cc: "guile-user@gnu.org" <guile-user@gnu.org>,
Dmitry Bogatov <KAction@gnu.org>
Subject: Re: Define in let
Date: Wed, 21 Aug 2013 08:52:02 +0200 [thread overview]
Message-ID: <CAMFYt2aEHVam9sPOpgvbtDiN=JT-O+495OeT=eJOwWyaZRXAAw@mail.gmail.com> (raw)
In-Reply-To: <20130820180137.301ace7e@capac>
[-- Attachment #1: Type: text/plain, Size: 1971 bytes --]
2013/8/20 David Pirotte <david@altosw.be>
> 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)))))
[-- Attachment #2: Type: text/html, Size: 2967 bytes --]
next prev parent reply other threads:[~2013-08-21 6:52 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-08-20 16:39 Define in let Dmitry Bogatov
2013-08-20 16:57 ` Thompson, David
2013-08-20 17:02 ` Taylan Ulrich B.
2013-08-20 17:18 ` John B. Brodie
2013-08-20 17:19 ` Ian Price
2013-08-20 17:52 ` Mike Gran
2013-08-20 21:01 ` David Pirotte
2013-08-21 6:52 ` Panicz Maciej Godek [this message]
2013-08-21 6:55 ` Panicz Maciej Godek
2013-08-21 9:28 ` Ralf Mattes
2013-08-21 10:17 ` Panicz Maciej Godek
2013-08-21 10:32 ` Ralf Mattes
2013-08-21 11:01 ` Panicz Maciej Godek
2013-08-21 15:42 ` Dmitry Bogatov
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/guile/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CAMFYt2aEHVam9sPOpgvbtDiN=JT-O+495OeT=eJOwWyaZRXAAw@mail.gmail.com' \
--to=godek.maciek@gmail.com \
--cc=KAction@gnu.org \
--cc=david@altosw.be \
--cc=guile-user@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).