skew70
2021-7-3 18:26:11

I have a hopefully easy macro question: how do I accomplish the following, correctly? #lang racket/base (define-syntax-rule (doit2) [(eq? 1 2) (printf "two")]) (cond [(eq? 1 3) (printf "three~%")] doit2 ; fails "cond: bad syntax (clause is not a test-value pair)" ;[doit2] ; also fails "doit2: use does not match pattern: (doit2)" [(eq? 1 1) (printf "one~%")]) I’m obviously missing something very basic with the macro processor. I’ve tried this a hundred different ways, including adding parentheses, removing parentheses, define-syntax-parse-rule, and even defmacro (hey, I’m desperate and frustrated, ok?), all to no avail. Help?


soegaard2
2021-7-3 18:28:10

Let’s figure out what you have written means.


soegaard2
2021-7-3 18:28:31

First, define-syntax-rule looks like: (define-syntax-rule (id . pattern) template)


soegaard2
2021-7-3 18:29:16

So in

(define-syntax-rule (doit2) [(eq? 1 2) (printf "two")]) The identifier id is doit2 and the pattern is empty.


soegaard2
2021-7-3 18:29:31

That means that the template is [(eq? 1 2) (printf "two")].


soegaard2
2021-7-3 18:30:12

Ah, so your idea is to write (doit2) in a cond and then get a cond-clause ?


skew70
2021-7-3 18:30:36

yes.


soegaard2
2021-7-3 18:30:43

The issue is that macro applications can’t appear anywhere.



sorawee
2021-7-3 18:31:29

At least the introduction


soegaard2
2021-7-3 18:32:10

The syntax of a cond expression is (cond clause ...). and it is up to cond to determine what a clause is.


soegaard2
2021-7-3 18:32:58

A cond-clause always have the form [expr expr ...] so you can’t put a macro call like (doit2) instead of a clause.


skew70
2021-7-3 18:34:54

I can check it out. The problem I’m trying to solve is I have a cond with say 10 clauses, but 5 of them are very repetitious. I just want to “macro them away”. My mental model (probably incorrect!) is that I can write a macro to scribble on what the compiler sees and compiles. Thanks for the reference.


sorawee
2021-7-3 18:35:40

I think there are two solutions to your problem


sorawee
2021-7-3 18:36:17

First one (which is what I would normally do) is to write your own cond, say, my-cond, that knows how to cooperate with the clauses


sorawee
2021-7-3 18:36:53

Second one is to simply use Alexis’s macro at the end of the blog post above.