guile-user
[Top][All Lists]
Advanced

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

Re: Syntax-rules generate symbol


From: Ian Price
Subject: Re: Syntax-rules generate symbol
Date: Tue, 10 Sep 2013 11:33:41 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.1 (gnu/linux)

Dmitry Bogatov <address@hidden> 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"



reply via email to

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