I have code like this:
(define-syntax macron
(syntax-rules ()
((_ name)
(lambda (x)
(eval (cons 'name x) (interaction-environment))))))
(define x (map (macron lambda)
'(((x) (display x)) ((a b) (+ a b)))))
(let ((square (car x))
(sum (cadr x)))
(display (square 10))
(newline)
(display (sum 1 2 3))
(newline))
the code is working it use macro as value by wrapping it with lambda. My question is how can I put inside syntax-rule macro literal symbol 'name instead of (cons 'lambda ...) so the output code is:
(lambda (x)
(eval (cons 'name x) (interaction-environment)))
so it work with code like this:
(define (name x)
(display x)
(newline))
(for-each (macron lambda) ;; lambda can be anything
'((1) (2) (3)))
and it print all the numbers.
I know that I can change the name in pattern into something else, but I want to know more about syntax-rules and it's edge cases. So is it possible to have name if I use it as input pattern?
I'm looking for answers with R7RS, that have more of this type of edge cases covered.
All macros happens in compile time so runtime stuff might not exist. That means that you should think of it as syntax sugar and use it as susch. eg.
Should then have an expansion based on that. Your current expansion is that it turns into this:
For
somethingbeing a macro this will apply the macro in runtime. It is bad. It also removes the need for the macro in the first place. You could do this instead:I made a programming language that had passable macros:
It's not a very good approach if you are targeting an efficient compiled end product. The first macros were indeed like this and they removed it in LISP 1.5 before Common Lisp. Scheme avoided macros for many years and opted for
syntax-rulesin R4RS as an optional feature. R6RS is the only version that has full power macros.With a procedure instead of macros this is actually the same as the following code with the bad
evalremoved:Which means you can implement
macronmuch easier:But from looking at this now you don't need a macro at all. This is partial application.
There is actually a SRFI-26 called
cut/cutewhich allows us to do something similar where it wraps it in a lambda:The
syntax-rulesare the macros with the least power. You cannot do anything unhygienic and you cannot make new identifiers based on other ones. Eg. it' impossible to implement a racket stylestructwhere you can do(struct complex [real imag])and have the macro createcomplex?,complex-real, andcomplex-imagas procedures. You need to do as SRFI-57 does and require th euser to specify all the names such that you don't need to concatenate to new identifiers.Right now R7RS-small only has
syntax-rules. I think it was a mistake not to have a more powerful macro as an alternative since now the R7RS-large cannot be implemented with R7RS-small.