From: Ken A. <kan...@bb...> - 2003-05-30 13:47:25
|
At 05:35 PM 5/28/2003 -0400, Geoffrey Knauth wrote: >I was looking at this: > >(define-macro (test-equal? x y) > `(let (([x-result] ,x) > ([y-result] ,y)) > (if (not (equal? [x-result] [y-result])) > (begin (display "Assertion failed!") (newline) > (display ',x) (display "->") (newline) > (write [x-result]) (newline) (display "does not equal") (newline) > (display ',y) (display "->") (newline) > (write [y-result]) (newline))))) > >and I wondered what the difference was between this and: > >(define (test-equal? x y) > (let (([x-result] x) > ([y-result] y)) > (if (not (equal? [x-result] [y-result])) > (begin (display "Assertion failed!") (newline) > (display 'x) (display "->") (newline) > (write [x-result]) (newline) (display "does not equal") (newline) > (display 'y) (display "->") (newline) > (write [y-result]) (newline))))) > >It started with my wondering why this was a macro and not a function, and now I'm wondering about the [ ] I see. In PLT Scheme, [ ] are used interchangeably with ( ), but in JScheme, [ ] are used to escape out of quasistrings, usually, except [x-result] is not a string. Does a foo argument for x or y get expanded to be foo-result? What happens to the [ ] in [x-result] ? Good question. the code i sent you was from 2001. It should mostly work, except for treatment of the characters "{}[]", that are currently handled specially by the reader, "{" is input to special string syntax, and "[" is escape into a scheme value. The cool thing is that all of this can be nested, though it breaks old code. A macro that uses [x-result] is probably trying to have the macro avoid capturing variables, because JScheme macros are unhygienic, like Common Lisp, unlike Scheme. So in JScheme you should need to use some likely to be unique name, like ***-x-result-***, or rewrite the macro to use a gensym that produces a unique name. While JScheme's builtin macro facility is like Common Lisp's - hurt yourself macros, there are 2 hygenic macro systems that work with JSCheme. The difference between the two examples you give is that the first is a macro so it can use x and y as both expressions and as the scheme value of those expressions by using ,x or ,y. So references to ',x is what x looks like, and ,x means the value of x. And in the second, because it is a function, you can only use the values of x and y. |