chicken-users
[Top][All Lists]
Advanced

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

Re: [Chicken-users] New to Scheme Macros


From: Thomas Chust
Subject: Re: [Chicken-users] New to Scheme Macros
Date: Wed, 22 Apr 2009 13:32:09 +0200

2009-04-22 Jordan Cooper <address@hidden>:
> [...]
> 1) I've used macros before in CL, but only getting into them for the
> first time here in Chicken. Is define-syntax the proper thing to use
> to do this?

Hello,

depending on your definition of "proper", the answer is probably yes
;-) At least using define-syntax and syntax-rules is the standard way
of defining macros in Scheme since R5RS.

> 2) define-syntax looks to be standard to all schemes. Can you do
> everything with it that you can do with Common Lisp macros?

There are still a bunch of Schemes out there that don't support
define-syntax or where it doesn't integrate too well with the rest of
the language implementation.

You also cannot do everything that is possible with Common Lisp macros
using syntax-rules macros. But this is supposed to be an advantage:
Macros implemented using syntax-rules are fully hygienic, ie. you
don't have to care about creating all those local names with gensym or
wonder whether some definition used in the macro might be shadowed by
user code around its invocation, you just write down the syntax-rules
macro and everything concerning scoping of variables just works right.
At least in theory.

In practice there are often pitfalls connected with the expansion of
macros and separate compilation of sources or the usage of module
systems. The latest Scheme standard R6RS tries to address this problem
by specifying a module system and its interaction with macros, but not
many full blown Scheme implementations support this, yet.

Also, if you want to break hygiene in your macros, you cannot do that
using syntax-rules but have to use either the classical Common Lisp
like macros or some extension like the syntax-case construct
implemented by many Schemes. syntax-case has become standard with
R6RS, too, but there are also some Scheme implementations that have
macro systems based on explicit renaming or syntactic closures and
expose those lower level optional hygiene breaking techniques to the
programmer instead of or in addition to syntax-case.

> 3) If the answer to #2 is "yes," is it as convenient?

Hygienic macros are often even more convenient to write than Common
Lisp style macros, since you save typing for the implementation of
identifier hygiene and you have pattern matching available to specify
the shape of input expressions to be transformed in a concise way.

> 4) If the answer to either #2 or #3 is "no," is there a way to get
> CL-style macros in chicken?
> [...]

Yes, CHICKEN supports define-macro in a way very similar to Common Lisp.

cu,
Thomas

-- 
When C++ is your hammer, every problem looks like your thumb.




reply via email to

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