I'm learning the syntax-case library and I don't understand a problem
I'm having at the moment. I hope someone is kind enough to point me
into the right direction. I have the following code:
(define-syntax test
(lambda (x)
(syntax-case x ()
((_ name1 name2)
(syntax
(define (name1)
(let ()
(define self
(lambda ()
(test2)))
(define (name2)
(display "hello world")
(newline))
self)))))))
(test test1 test2)
(let ((obj (test1)))
(obj))
This throws a test2 unbound variable exception. If I change name2 in
the syntax expression to test2 it does work but that's not what I
want. The part that's not clear to me is why name1 (= test1) can be
called while name2 (= test2) apparently cannot be called. They are
defined in a similar way.
Cheers,
Mark
The identifier test2 in the expansion of a use of the macro test
refers to the binding of test2 at the place of the macro definition.
Since you haven't bound test2 above, you get the unbound variable
error. Try the following to see what I mean:
(define (test2)
(display "Hello")
(newline))
(define-syntax test
(lambda (x)
(syntax-case x ()
((_ name1 name2)
(syntax
(define (name1)
(let ()
(define self
(lambda ()
(test2)))
(define (name2)
(display "hello world")
(newline))
self)))))))
(test test1 test2)
(let ((obj (test1)))
(obj))
--
Jens Axel Søgaard
> This throws a test2 unbound variable exception. If I change name2 in
> the syntax expression to test2 it does work but that's not what I
> want. The part that's not clear to me is why name1 (= test1) can be
> called while name2 (= test2) apparently cannot be called. They are
> defined in a similar way.
Forgot to say, that you probably want to do as follows:
(define-syntax test
(lambda (x)
(syntax-case x ()
((_ name1 name2)
(syntax
(define (name1)
(let ()
(define self
(lambda ()
(name2)))
(define (name2)
(display "hello world")
(newline))
self)))))))
(test test1 test2)
(let ((obj (test1)))
(obj))
--
Jens Axel Søgaard
Thanks, this is an explanation I was looking for. I already found the
solution proposed by you in your second post, but now I know why it
works that way.
Mark
(define-syntax test
(lambda (x)
(syntax-case x ()
((_ name1 name2)
(syntax
(define (name1 #!rest rest)
(let ()
(define self
(lambda ()
(name2)))
(define (name2)
(display "hello world")
(newline))
self)))))))
(test test1 test2)
(let ((obj (test1)))
(obj))
the macro expansion thinks #!rest is a variable, but it shouldn't be
one. How can I work around this?
A little more context is needed to answer that question.
Which implementation do you use?
(Since #!rest is non-standard most Scheme implementations
will interpret #!rest a variable name. Since you expecting
something different, you must be using an implementation
that has given #!rest another interpretation)
--
Jens Axel Søgaard
Chicken.
I tried your example in Chicken.
The portable syntax-case implementation, doesn't support rest-arguments.
Since the Chicken implementation of syntax-case is derived from the
portable one, my *guess* is that rest-arguments were forgotten.
To get a better answer, try posting your example at the Chicken
mailing list.
--
Jens Axel Søgaard
I'll try that. Thanks for your prompt and useful responses.