All Questions
220
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:
...
50
votes
5
answers
8k
views
Lazy Evaluation vs Macros
I'm used to lazy evaluation from Haskell, and find myself getting irritated with eager-by-default languages now that I've used lazy evaluation properly. This is actually quite damaging, as the other ...
30
votes
6
answers
6k
views
Can someone explain the concept of 'hygiene' to me (I'm a scheme programmer)?
So... I'm new to scheme r6rs, and am learning macros. Can somebody explain to me what is meant by 'hygiene'?
Thanks in advance.
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 ...
27
votes
3
answers
6k
views
What are the advantages of scheme macros?
Why would anyone prefer Scheme macros over Common Lisp macros (and I genuinely want to know too, I'm not trying to be a troll)?
My experience as a Lisp newb is that Common Lisp style macros are much ...
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 ...
23
votes
3
answers
6k
views
Is it possible to implement Common Lisp's macro system in scheme?
Hopefully this is not a redundant question.
As a newcomer to scheme I am aware that syntax-case macros are more powerful than the syntax-rules alternative, at the cost of unwanted complexity.
Is ...
21
votes
16
answers
16k
views
Python Macros: Use Cases?
If Python had a macro facility similar to Lisp/Scheme (something like MetaPython), how would you use it?
If you are a Lisp/Scheme programmer, what sorts of things do you use macros for (other than ...
20
votes
6
answers
1k
views
To what extent are macros "functions in reverse?"
I'm writing a Lisp in Haskell (code at GitHub) as a way of learning more about both languages.
The newest feature that I'm adding is macros. Not hygienic macros or anything fancy - just plain vanilla ...
20
votes
5
answers
3k
views
What are the practical differences between special forms and macros?
Are there any practical differences between special forms and macros? In what do they differ?
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 ...)`
'(...
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
1k
views
Defining the defmacro function using only LISP primitives?
McCarthy's Elementary S-functions and predicates were atom, eq, car, cdr, cons
He then went on to add to his basic notation, to enable writing what he called S-functions: quote, cond, lambda, label
...
11
votes
4
answers
6k
views
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 ...
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 ...
9
votes
2
answers
1k
views
How do you write an MIT Scheme macro to return a lambda form?
I'm baffled by trying to create the equivalent of this trivial (in Common Lisp) macro in MIT Scheme:
(defmacro funcify (exp)
`(lambda (x) ,exp))
This is for a simple personal project, a ...
8
votes
1
answer
976
views
Does adding f-exprs simplify the implementation of macros from fundamental expressions in LISP?
This guy reckons that adding fexprs to LISP dramatically simplifies the implementation of the language in scheme.
Here we see how macros would be implemented using MacCarthy's LISP.
My question is ...
8
votes
1
answer
1k
views
What is it about a single namespace that leads to unhygienic macros? (in LISP)
Some claim that a single namespace in LISP leads to unhygienic macros.
http://community.schemewiki.org/?hygiene-versus-gensym
http://www.nhplace.com/kent/Papers/Technical-Issues.html
What precisely ...
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
2
answers
413
views
Scheme macro triggered by keyword which is not the head of a list
Suppose I want to trigger a Scheme macro on something other than the first item in an s-expression. For example, suppose that I wanted to replace define with an infix-style :=, so that:
(a := 5) ->...
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
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 ...
7
votes
1
answer
174
views
Extensible macro definitions
Inspired by a comment thread on a related question regarding functions instead of macros.
Is there any way to extend a Scheme syntax definition so that it can use the previous definition of the ...
6
votes
6
answers
5k
views
Converting a Scheme expression to a string
Given an expression '(lambda (x) x) How can I translate this into a string. I thought symbol->string will do the job but no it cant not a symbol.
e.g for a macro to-string:
(to-string (lambda(x) x))...
6
votes
7
answers
992
views
Are there whole-program-transforming macros in Lisp or Scheme?
I have seen one answer of How does Lisp let you redefine the language itself?
Stack Overflow question (answered by Noah Lavine):
Macros aren't quite a complete redefinition of the language, at ...
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
5
answers
520
views
Are incremental Macro definition possible?
I often find the following type of incremental definition useful:
(define (foo) (display "bar"))
(foo)
;prints bar
(define foo (let ((bar foo))
(lambda ()
(display "foo")...
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)...
6
votes
2
answers
787
views
SICP: Can or be defined in lisp as a syntactic transformation without gensym?
I am trying to solve the last part of question 4.4 of the Structure and Interpretation of computer programming; the task is to implement or as a syntactic transformation. Only elementary syntactic ...
6
votes
1
answer
998
views
`amb` operator as macro or procedure?
In this page there is a comment after the post that gives a very short implementation of amb as a procedure:
(define (amb-backtrack)
(error "no solution found"))
(define (amb . args)
(call/cc (...
6
votes
1
answer
219
views
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:
(...
5
votes
4
answers
188
views
or as procedure in scheme
I want to apply or to every element of list, example:
(apply or '(#t #t #f)) expected result #t, but I'm getting error:
'#' to 'apply' has wrong type (kawa.lang.Macro) (expected: procedure, ...
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
1
answer
1k
views
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. ...
5
votes
4
answers
1k
views
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, ...
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
3
answers
732
views
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 ...
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
170
views
syntax-rules not completely hygienic?
I understand that syntax-rules is a hygienic macro system, but I do not understand why this happens:
(define not (lambda (x) x))
(define-syntax nand
(syntax-rules ()
((_ a b)
(not (and a ...
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 ...
5
votes
2
answers
902
views
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 ()
((_ () ...
5
votes
3
answers
860
views
How do I map a macro across a list in Scheme?
I have a Scheme macro and a long list, and I'd like to map the macro across the list, just as if it were a function. How can I do that using R5RS?
The macro accepts several arguments:
(mac a b c d)
...
5
votes
1
answer
491
views
Implicit currying in Scheme with syntax-rules?
Jeffrey Meunier has an implicit Curry macro here, which uses defmacro. I was wondering if someone has ever written this with syntax-rules?
5
votes
3
answers
290
views
Macros and internal definitions in Scheme
A good question was asked on Freenode's #scheme channel. Consider the following code in Scheme:
(define alpha 1)
(define-syntax foo
(syntax-rules (quote alpha)
((_ alpha msg) (define bar 2))
...
4
votes
2
answers
1k
views
Learn Macros in Scheme from On Lisp [closed]
I really want to learn Scheme macros. I glanced over the content of "On Lisp" and a lot of the chapters have been devoted to Lisp macros. However I do not know common lisp. Can I use it to learn ...
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 ...