Racket has built-in fundamental form 2-arm if, but it doesn't have the word else, so I want to add the else word to it.
This code works:
(require syntax/parse/define)
(define-syntax-rule (myif Cond Form1 else Form2)
(if Cond Form1 Form2)
)
(myif #t (displayln 1) else (displayln 2))
However myif is undesired as keyword, changing it to if raises error:
if: use does not match pattern: (if Cond Form1 else Form2)
in: (if #t (displayln 1) (displayln 2))
How to redefine the form if?
The answer above is "correct" at some level, but there are some mistakes.
define-syntax-ruleis not fromsyntax/parse/define. If you want to usedefine-syntax-rule, there's no need to(require syntax/parse/define).What will happen when I call
(if #t 1 2 3)with yourif?elsein your macro is a pattern variable, so it can match against anything. Do you intend to allow this to happen? If not, this is where you can use the features ofsyntax/parse/define. You can write:So
(if 1 then 2 else 3)will expand to(racket:if 1 2 3), but(if 1 0 2 0 3)will fail with an error.