Questions tagged [define-syntax]
42 questions
11
votes
4 answers
Sources for learning about Scheme Macros: define-syntax and syntax-rules
I've read JRM's Syntax-rules Primer for the Merely Eccentric and it has helped me understand syntax-rules and how it's different from common-lisp's define-macro. syntax-rules is only one way of implementing a syntax transformer within…

Kyle Burton
- 26,788
- 9
- 50
- 60
7
votes
3 answers
Capturing Macros in Scheme
What's the simplest way to define a capturing macro using define-syntax or define-syntax-rule in Racket?
As a concrete example, here's the trivial aif in a CL-style macro system.
(defmacro aif (test if-true &optional if-false)
`(let ((it…

Inaimathi
- 13,853
- 9
- 49
- 93
6
votes
2 answers
Can "if" be implemented using "call/cc"?
I've been told that "call/cc" can be used to implement arbitrary control flow constructs so I'm trying to implement all such constructs using "call/cc" but I'm having trouble. Assuming I didn't have "if" how would I implement it using…

N4tur41Myst1c
- 63
- 3
6
votes
1 answer
How are vector patterns used in syntax-rules?
I have been writing Common Lisp macros, so Scheme's R5Rs macros are a bit unnatural to me. I think I got the idea, except that I don't understand how one would use vector patterns in syntax-rules:
(define-syntax mac
(syntax-rules ()
((mac #(a…

Jay
- 9,585
- 6
- 49
- 72
6
votes
1 answer
Is there any way to define a compile-time (expansion-time) macro variable in Racket or any other Scheme?
To give a simple example:
(define-macro-variable _iota 0) ; define-macro-variable does not really exist
(define-syntax (iota stx)
(syntax-case stx ()
((iota)
(let ((i _iota))
(set! _iota (+ i 1))
#`#,i))))
Such that…

Matt
- 21,026
- 18
- 63
- 115
5
votes
1 answer
what's wrong with this define-syntax macro in scheme?
I'm working though SICP and wanted to try out some of the examples in guile. I'm trying the stream examples and wanted an implementation for cons-stream, which I got from this StackOverflow question. However when I type this into guile I get:
guile>…

Tom Carver
- 962
- 7
- 17
5
votes
2 answers
Racket macro that defines multiple top-level forms?
I found myself defining syntax parameters with identical definitions except for their name so I decided to write a macro to make this simpler:
(define-syntax (test-case-parameter stx)
(syntax-parse stx
[(_ parameter:id)
…

Joseph Garvin
- 20,727
- 18
- 94
- 165
5
votes
3 answers
Scheme Macro for nesting expressions
Can a macro be written in Scheme (with define-syntax, for example) which will take expressions like this:
(op a b c d e f g h i j)
And yield expressions like this as output?
(op (op (op (op (op (op (op (op (op a b) c) d) e) f) g) h) i) j)
Of…

Claudiu
- 224,032
- 165
- 485
- 680
5
votes
4 answers
What, if any, is wrong with this definition of letrec in Scheme?
R5RS gives proposed macro definitions for library forms of syntax:
http://schemers.org/Documents/Standards/R5RS/HTML/r5rs-Z-H-10.html#%_sec_7.3
Which also defines letrec, in a very complicated way, certainly not how I would define it, I would simply…

Zorf
- 6,334
- 2
- 29
- 24
4
votes
2 answers
Racket macro for expanding code
I want to be able to write:
(nota E2 82)
instead of:
(define E2
(network ()
[sunet <= sine-wave 82]
[out = (+ sunet)]))
I know I can do this using macros and tried to write this:
(define-syntax (nota stx)
(syntax-case stx…

Theodor Berza
- 573
- 3
- 12
4
votes
2 answers
Currying functions in Scheme using macros
I'm learning about the macro system in Scheme and I thought implementing curried functions would be a good start. This is what I cooked up:
(define-syntax function
(syntax-rules ()
((_ () body ...) (lambda () body ...))
((_…

Aadit M Shah
- 72,912
- 30
- 168
- 299
3
votes
2 answers
Racket macros - making pairs
I've just started diving into Racket macros, and am trying to make a terse simple-macro-defining macro. I would like to expand an expression like this:
(macro id
(param) replacement1
(params ...) replacement2)
Into something like…

twf
- 325
- 4
- 8
3
votes
1 answer
Writing a `define-let` macro, with hygiene
I'm trying to write a define-let macro in racket, which "saves" the header of a (let ((var value) ...) ...) , namely just the (var value) ... part, and allows re-using it later on.
The code below works as expected:
#lang racket
;; define-let allows…

Suzanne Soy
- 3,027
- 6
- 38
- 56
2
votes
2 answers
Disallow C preprocessor from using a macro within another
I'm experimenting to see how far I can abuse the C preprocessor and I have stumbled across an interesting problem.
I have the following macro defines:
#define if(x) if (x)
#define do {
#define elif(x) } else if (x) {
#define else } else…

agregate
- 163
- 6
2
votes
3 answers
C++: Simplifiying a #define
I have a #define with generates a enum class and a corresponding output operator the the generated enum class.(see below)
#define ENUM(N, T, N1, V1, N2, V2, N3, V3, N4, V4, N5, V5, N6, V6, N7, V7)\
enum class N : T {\
N1 = V1,\
…

Rainer
- 51
- 5