All Questions
171
questions
60
votes
8
answers
3k
views
Collection of Great Applications and Programs using Macros
I am very very interested in Macros and just beginning to understand its true power. Please help me collect some great usage of macro systems.
So far I have these constructs:
Pattern Matching:
...
29
votes
3
answers
2k
views
When did the idea of macros (user-defined code transformation) appear?
I have read McCarthy's 1960 paper on LISP and found no reference to anything that's similar to user-defined macros or normal order evaluation. I was wondering when macros first appeared in programming ...
24
votes
3
answers
3k
views
Racket vs Scheme macros
Do racket macros have more advanced functionality than those found in Scheme or Common Lisp? I suspect so, especially regarding modules, namespaces and scoping, etc, but I'd appreciate a simple ...
19
votes
2
answers
2k
views
Racket reader macros
Is there any way to make simple reader macros in Racket. I mean a generalization like this:
(define-reader-syntax "'" quote)
; finds expressions that start with "'" and wraps them in `(quote ...)`
'(...
15
votes
1
answer
625
views
What is "3D syntax"?
In the context of writing Racket macros, what does "3D syntax" mean?
I've heard the phrase a few times. Including once in reference to a macro I was writing. But that was awhile ago; I fixed it, and ...
13
votes
10
answers
4k
views
What are some things that you've used Scheme macros for? [closed]
Many examples of macros seem to be about hiding lambdas, e.g. with-open-file in CL. I'm looking for some more exotic uses of macros, particularly in PLT Scheme. I'd like to get a feel for when to ...
13
votes
1
answer
953
views
Can Racket macros take keyword arguments?
I'd like to create a syntactic form in Racket that can accept a keyword argument, the way some functions can.
Having reduced it to a simple example, I tried writing:
(define-syntax sum-of-products
...
12
votes
2
answers
739
views
Should macros have side effects?
Can (or should) a macro expansion have side effects? For example, here is a macro which actually goes and grabs the contents of a webpage at compile time:
#lang racket
(require (for-syntax net/url))
...
10
votes
2
answers
2k
views
Capturing a variable number of arguments via an ellipsis in a nested macro; Missing pattern variable error
Consider a scenario of two macros: the outer-macro defines a general structure of some entity, and the inner-macro expands in the scope of the outer macro. My intent is captured in the following code, ...
9
votes
1
answer
3k
views
How do I define functions using Racket macros?
I am trying to write a macro that defines a special class of data structure with associated functions.
I know this is possible; it is done multiple times in the core language itself.
As a specific ...
8
votes
1
answer
785
views
What is difference between datum->syntax and syntax #' in define-syntax body?
Testing code:
(define-syntax (test-d stx)
#'(begin
(define (callme a)
(writeln a))))
(define-syntax (test-e stx)
(datum->syntax stx '(begin
(define (...
7
votes
3
answers
1k
views
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 (...
7
votes
3
answers
843
views
Macro-defining macro in Racket?
In Common Lisp it is relatively easy to create a macro-defining macro. For example, the following macro
(defmacro abbrev (short long)
`(defmacro ,short (&rest args)
`(,',long ,@args)))
is ...
7
votes
2
answers
670
views
define-match-expander
about the define-match-expansion, there are rare materials and example codes to illustrate the concepts. I am having a hard time to "decode" what the documentation says:
(define-match-expander id ...
7
votes
1
answer
93
views
When should I use `protect-out` in Racket?
Racket provides protect-out to prevent module exports from being used with eval (or a deconstructed syntax object), unless the module has enough privileges (aka, has a strong enough code inspector). ...
7
votes
1
answer
80
views
RackUnit source location inside of macros
I am building a set of rackunit tests, where the actual test-case and check-equal? function is defined in a macro. The code looks something like this:
#lang racket
(require rackunit
...
7
votes
3
answers
180
views
How can I group optional attributes captured with syntax-parse?
When writing a macro that uses syntax/parse, I have created a splicing syntax class that captures options that may be provided to the macro. These options are all optional, and they may be provided in ...
6
votes
1
answer
416
views
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! ...
6
votes
1
answer
471
views
When is macro expansion performed?
I'm learning about macros in Racket (language successor of Scheme). There is no mentioning of when the macro expansion is performed. On page 17 of this document I found a paragraph that says it ...
6
votes
1
answer
574
views
How to pass `and` as a function in Racket?
For the following code:
(foldl and #t '(#t #f))
Racket returns:
and: bad syntax in: and
I know and is not a function. And I can circumvent this problem using lambda:
(foldl (lambda (a b) (and a b)...
5
votes
3
answers
737
views
Macro that unrolls a 'for' loop in racket/scheme?
I'm trying to write a macro in racket/scheme that operates like a for loop across some arbitrary code such that the body of the loop is unrolled. For example, the following code
(macro-for ((i '(0 1 ...
5
votes
2
answers
2k
views
Macro stepper in DrRacket
On the link http://www.ccs.neu.edu/home/ryanc/macro-stepper/tutorial.html there are instructions for working with the macro stepper.
However, when I'm going to try it, I can't get the second ...
5
votes
1
answer
989
views
Writing an auto-memoizer in Scheme. Help with macro and a wrapper
I am facing a couple of problems while writing an auto-memoizer in Scheme.
I have a working memoizer function, which creats a hash table and checks if the value is already computed. If it has been ...
5
votes
2
answers
464
views
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 ...
5
votes
2
answers
1k
views
Scheme: difference between define and define-syntax-rule
I've been given two if-statements instructions in Racket:
(define (if-fun c thn els) (if c thn els))
(define-syntax-rule (if-mac c thn els) (if c thn els))
Would someone please mind explaining the ...
5
votes
1
answer
59
views
Ascending Numbers in Macro Definition
I frequently use Racket's pattern-matching construct match, and I thought a way to help myself with debugging programs using match, and to learn how Racket/Scheme macros work, would be to create a ...
4
votes
3
answers
1k
views
For-loop macro in Racket
This macro to implement a C-like for-loop in Lisp is mentioned on this page: https://softwareengineering.stackexchange.com/questions/124930/how-useful-are-lisp-macros
(defmacro for-loop [[sym init ...
4
votes
3
answers
543
views
Scheme/Racket - Macro to change order of procedure an arguments
I'd like to change the syntax of the following expression:
(> 2 1)
to something like:
(2 greater 1)
My first try is the following macro:
(define-syntax greater
(lambda (x)
(syntax-case ...
4
votes
4
answers
6k
views
While Loop Macro in DrRacket
I am trying to create a macro for while loop in DrRacket. Here is what I wrote:
(require mzlib/defmacro)
(define-macro my-while
(lambda (condition body)
(list 'local (list (list 'define (list ...
4
votes
2
answers
401
views
Mutable versions of cadr, caddr, etc
I'm wondering how to implement mutable versions of cadr, caddr, and the likes in Racket without defining each one separately? ie. not
(define (mcadr exp)
(mcar (mcdr exp)))
It seems that for ...
4
votes
2
answers
349
views
racket: macro expand inside match pattern
Is there any way to detect if a macro is expanding inside a pattern match?
Here's an example macro that I'd like to write, but it fails inside a match-define:
#lang racket/base
(require racket/match ...
4
votes
2
answers
710
views
set! global from Scheme macro?
I am trying to write a wrapper for define, that stores the values passed to it. I've been approaching it in baby steps (being new to Lisp in general, and even newer to Scheme) but have run into a ...
4
votes
2
answers
92
views
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 ...
4
votes
3
answers
849
views
How can this racket code for an anaphoric -> or ->> macro be improved?
I want to create a Racket macro which includes the functionality of one of
Clojure's threading macros, but in addition requires an anaphoric argument
(for example it), permitting me to specifically ...
4
votes
2
answers
2k
views
Racket Macro Ellipsis Syntax
I have a macro that's working when one argument is passed, and I'd like to expand it to accept n number of arguments using ..., but I'm having trouble figuring out the syntax.
The macro accepts ...
4
votes
1
answer
182
views
Scheme: macros or higher order functions for code duplication?
I would like to get the results of a function which calls a shell command and returns a string.
I am using racket, and this is my first attempt:
(define (run-function)
(let*
([stdout (some-...
4
votes
1
answer
403
views
Language extension using Racket, defining helper functions via macros
I've been stuck with an issue for a number of hours now. I'm trying to define a DSL using Racket's language extension features. I want to do something like the following pseudo-code. Ultimately I'd ...
4
votes
1
answer
2k
views
How do you initialize a struct with default values in typed/racket?
I'm writing my first typed/racket application which is a cli application which asks you some statistical questions about your day such as how many hours you have slept the previous night, how much you ...
4
votes
1
answer
95
views
Unquoting from a Racket macro
I have a function that does some processing on an expression:
(struct sym (s) #:transparent)
(define (foo-fn ex)
(match ex
[(? symbol? s) `(sym (quote ,s))]
[(? list? xs) (cons (car xs) (...
4
votes
1
answer
74
views
What is the difference between visiting, instantiating, and declaring a module?
Functions in racket such as module->language-info, module->imports, and module->exports expect their module to be declared, but not necessarily visited or instantiated.
Now, dynamic-require ...
4
votes
1
answer
960
views
Racket Macro How to Pass Ellipses to Helper function?
Given:
(define-syntax (test stx)
(syntax-case stx ()
[(_ body ...)
(with-syntax ([body0 (process-body #'(body ...))])
#'body0)]))
How should I receive the pattern and the ellipses ...
4
votes
2
answers
156
views
Racket - Having trouble with variables in macro
I'm currently trying to assign a variable to macro to store something:
(begin-for-syntax
(define a 0))
(define-syntax (foo stx)
(set! a (+ a 1))
(datum->syntax stx a))
(foo)
(foo)
(foo)
...
4
votes
1
answer
372
views
Racket / Scheme - Syntax-Case
I'm currently working on an assigment about racket macros. In one of the questions we`re asked to define a macro my-set! which acts like this:
(define x 3)
(define y 5)
(define z 7)
(my-set! (x (+ x ...
4
votes
3
answers
383
views
Racket with-hash macro and rename transformers
I created this:
(define-syntax (with-hash stx)
(syntax-parse stx
[(_ obj:id ((~or key:id [new-key:id hash-key:id]) ...) body:expr ...+)
#'(let ([key (hash-ref obj 'key)] ...
[...
4
votes
0
answers
230
views
How to require threading macros
How exactly do I require threading macros in Racket?
Following this guide: https://docs.racket-lang.org/threading/index.html I try (require threading) but I get this error:
standard-module-name-...
3
votes
4
answers
411
views
Is it possible to write a function that would take any macro and turn it into a function so that it can be passed as an argument to another function?
AND and OR are macros and since macros aren't first class in scheme/racket they cannot be passed as arguments to other functions. A partial solution is to use and-map or or-map. Is it possible to ...
3
votes
4
answers
545
views
Why aren't lisp macros eagerly expanded by default?
Say I have macros foo and bar. If I write (foo (bar)) my understanding is that in most (all?) lisps foo is going to be given '(bar), not whatever bar would have expanded to had it been expanded first. ...
3
votes
2
answers
456
views
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 ....
3
votes
2
answers
542
views
Racket "match" construct - is it a macro?
Simple question - is the match form in Racket a macro? It certainly seems like it could be defined as a macro, but I thought it might be baked further into the implementation to make it faster or ...
3
votes
2
answers
671
views
How do I rename a procedure with macros in Racket?
Let's say I want to replace all occurrences of an procedure at compile time, for example all occurrences of cons with 😄. I tried two options that seemed natural:
1.
(define-syntax 😄
(syntax-...