From: Ian Price <ianprice90@googlemail.com>
To: Dmitry Bogatov <KAction@gnu.org>
Cc: "guile-user@gnu.org" <guile-user@gnu.org>
Subject: Re: Syntax-rules generate symbol
Date: Tue, 10 Sep 2013 11:33:41 +0100 [thread overview]
Message-ID: <878uz5neyi.fsf@Kagami.home> (raw)
In-Reply-To: <87txhuarxf.fsf@gnu.org> (Dmitry Bogatov's message of "Mon, 09 Sep 2013 20:19:40 +0400")
Dmitry Bogatov <KAction@gnu.org> writes:
> Hello!
>
> Here is my implementation of for loop. I found lisp really extremely
> flexible, but there is one problem --- more often then not I do not need
> var part, so I do not care how it would be named --- all I care is that
> it will not shadow any other bindings.
>
> I think I can do it(did not tryed it) with `define-macro` and uninterned
> symbols, but it mean give up beauty of syntax-rules.
>
> Masters of syntax-rules and syntax-case, please give me peace of advice.
>
> (define-syntax for
> (syntax-rules (in => as)
> ([_ (pattern as var in list) exp ...]
> [for-each (lambda (var) (match var (pattern exp ...))) list])))
Actually, it couldn't be simpler, just add an extra pattern that passes
in a dummy name. No (explicit) gensym needed.
(define-syntax for
(syntax-rules (in => as)
[(_ (pattern as var in list) exp ...)
(for-each (lambda (var) (match var (pattern exp ...))) list)]
[(_ (pattern in list) exp ...)
(for (pattern as var in list) exp ...)]))
Now we get
scheme@(guile-user)> (for (a in '(1 2 3)) (pk a))
;;; (1)
;;; (2)
;;; (3)
scheme@(guile-user)> (for (a in '(1 2 3)) (pk var))
;;; <stdin>:52:21: warning: possibly unbound variable `var'
<unnamed port>:52:0: In procedure #<procedure a90f4d0 at <current input>:52:0 (var)>:
<unnamed port>:52:0: In procedure module-lookup: Unbound variable: var
Entering a new prompt. Type `,bt' for a backtrace or `,q' to continue.
,expand (for (a in '(1 2 3)) (pk var))
$4 = (for-each
(lambda (var-1)
(let* ((v var-1)
(failure
(lambda ()
(((@@ (ice-9 match) error)
'match
"no matching pattern"
v))))
(a v))
(pk var)))
'(1 2 3))
Var is unbound as expected. Though this is not obvious if you use
,expand on an example that doesn't use 'var', since Guile tries to clean
up the names for readability.
The reason it's okay to just pass in the dummy is because of how
syntax-case and syntax-rules work. If a name is not passed explicitly
into the macro, and it is not a reference to a top-level function or
macro, then syntax-case or syntax-rules will rename it automatically.
I took the liberty of fixing up the indentation, and changing the use of
[] to a more idiomatic one, but there is still one obvious issue with
your macro. "exp ..." means 0 or more expressions. If you had intended
one or more, as is more usual in this type of macro, you'll want to say
so explicitly with something like "exp exps ...".
Cheers
--
Ian Price -- shift-reset.com
"Programming is like pinball. The reward for doing it well is
the opportunity to do it again" - from "The Wizardy Compiled"
prev parent reply other threads:[~2013-09-10 10:33 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-09-09 16:19 Syntax-rules generate symbol Dmitry Bogatov
2013-09-09 16:59 ` Panicz Maciej Godek
2013-09-09 20:03 ` Taylan Ulrich B.
2013-09-10 6:11 ` Panicz Maciej Godek
2013-09-10 9:16 ` Taylan Ulrich B.
2013-09-10 10:38 ` Ian Price
2013-09-10 10:33 ` Ian Price [this message]
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=878uz5neyi.fsf@Kagami.home \
--to=ianprice90@googlemail.com \
--cc=KAction@gnu.org \
--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).