All Questions
Tagged with macros common-lisp
344
questions
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 ...
26
votes
2
answers
8k
views
Given the following LISP eval function - what is required to add defmacro?
Given the following definition of the LISP eval function - what is required to add the defmacro function? (Or even just evaluate a macro)
(defun null. (x)
(eq x '()))
(defun and. (x y)
(cond ...
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 ...
23
votes
4
answers
4k
views
How do you compile macros in a Lisp compiler?
In a Lisp interpreter, there can easily be a branch in eval that can expand a macro, and in the process of expanding it, call functions to build up the expanded expression. I've done this before using ...
21
votes
2
answers
4k
views
Common Lisp Double-Backquote, Unquote, Quote, Unquote sequence?
I'm reading Let Over Lambda, which deals with some pretty deeply layered macro authoring. It's fascinating and I'm mostly managing to keep up with it.
In Chapter 4 Hoyte implements reader macros for ...
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?
18
votes
5
answers
11k
views
Macros, clojure vs common lisp
A few of my friends and I are working on a new platform and we want to build it in lisp. The main attraction are macros. We all use Common Lisp but I want to explore the option of Clojure.
When I ...
17
votes
3
answers
3k
views
Understanding how to implement once-only lisp macro
In Peter Seibel's book "Practical Common Lisp", we can find the definition of the very complicated macro once-only (see the bottom of page http://www.gigamonkeys.com/book/macros-defining-your-own.html)...
17
votes
2
answers
750
views
Is a "transparent" macrolet possible?
I'd like to write a Clojure with-test-tags macro that wraps a bunch of forms, and adds some metadata to the name of each deftest form - specifically, add some stuff to a :tags key, so that I can play ...
16
votes
2
answers
923
views
First Lisp with macros?
McCarthy's original Lisp and some number of incarnations thereafter did not have a macro facility like we now have in Common Lisp, Clojure, Scheme, etc... This I know.
However, it is unclear to me ...
15
votes
2
answers
2k
views
Is it possible in Lisp to undefine Macros and Functions?
While using the REPL it would be helpful to undefine defined functions and macros, exspecially if you tried to make a macro for something, and then simulate it as function, and the macro is called ...
15
votes
1
answer
3k
views
Eval-when uses?
After reading a lot of documentation regarding Lisp eval-when operator I still can't understand its uses, I know with this operator I can control the evaluation time of my expressions but I can't ...
14
votes
4
answers
4k
views
Expand a macro form completely
I'd like to learn the internals of Lisp, so I want to see how everything is implemented.
For example,
(macroexpand '(loop for i upto 10 collect i))
gives me (in SBCL)
(BLOCK NIL
(LET ((I 0))
...
14
votes
3
answers
377
views
Lisp unit tests for macros conventions and best practices
I find it hard to reason about macro-expansion and was wondering what the best practices were for testing them.
So if I have a macro, I can perform one level of macro expansion via macroexpand-1.
(...
13
votes
2
answers
2k
views
common lisp: how can a macro define other methods/macros with programmatically generated names?
I realized that a certain section of my code consists of groups of methods that look similar (like I have multiple trios: a helper function that gets called by two other functions meant for the ...
12
votes
3
answers
5k
views
Lisp: can a macro be recursive?
I've recently started coding in Lisp, and have already been most impressed with macros - they allowed me to do complex loop-unrolling at compile-time, something I can't do this elegantly in any other ...
12
votes
3
answers
2k
views
Renaming lambda in Common Lisp
I started learning Common Lisp recently, and (just for fun) decided to rename the lambda macro.
My attempt was this:
> (defmacro λ (args &body body) `(lambda ,args ,@body))
It seems to ...
11
votes
3
answers
10k
views
Lisp: Macros vs Functions [duplicate]
In my quest to fully understand the so powerful lisp macros a question came to my mind. I know that a golden rule about macros is the one saying "Never use a macro when a function will do the work".
...
10
votes
5
answers
4k
views
what is to append as push is to cons, in Lisp?
(push x list)
expands to
(setq list (cons x list))
What expands to the following:
(setq list (append list2 list))
? Is there a standard macro for this?
10
votes
2
answers
815
views
In Common Lisp why does the macro OR use a gensym, but not AND?
In Common Lisp (SBCL 1.0.58) why does the macro OR use a gensym, but not AND?
For example,
CL-USER> (macroexpand '(and 1 2 3 4 5))
(IF 1
(AND 2 3 4 5)
NIL)
T
CL-...
10
votes
2
answers
945
views
How do I write a macro-defining macro in common lisp
I have about two macros (and climbing) in my codebase that look like this:
(defmacro def-stat-method (method-name stat)
`(progn
(defmethod ,method-name ((monster monster))
(getf (stats ...
10
votes
2
answers
3k
views
How is the defun macro implemented in lisp?
I'd like to learn more about lisp macros and I want to create a simple implementation of the defun macro.
I'm also interested in lisp's source code in all the implementations.
9
votes
4
answers
2k
views
Common Lisp recursive macro expansion
Once upon a time I was playing with macros and came up with this:
(defmacro my-recursive-fact (n)
(if (= 0 n) '1
(let ((m (1- n)))
`(* ,n (my-recursive-fact ,m)))))
And it worked.
CL-...
9
votes
5
answers
719
views
How do recursive macro definitions get evaluated
This recursive definition of a macro does what it should (sum integers from 1 to n):
(defmacro sum-int-seq (n)
`(cond
((equal 0 ,n) 0)
(t (+ ,n (sum-int-seq (- ,n 1))))))
For example (...
9
votes
3
answers
6k
views
Common Lisp Backquote/Backtick: How to Use?
I am having trouble with Lisp's backquote read macro. Whenever I try to write a macro that seems to require the use of embedded backquotes (e.g., ``(w ,x ,,y) from Paul Graham's ANSI Common Lisp, ...
9
votes
3
answers
532
views
Macros That Write Macros - Compile Error
When I compile the following code, SBCL complains that g!-unit-value and g!-unit are undefined. I'm not sure how to debug this. As far as I can tell, flatten is failing.
When flatten reaches the ...
9
votes
1
answer
189
views
Difference between `(&rest xs)` and `xs` in defmacro formal argument list
In Practical Common Lisp's Chapter 8, Macros: Defining Your Own, we define a macro with-gensyms as follows:
(defmacro with-gensyms ((&rest names) &body body)
`(let ,(loop for n in names ...
8
votes
3
answers
357
views
Why is it necessary to recompile the definitions that use a Common Lisp macro when we modify it?
This is one doubt that always made me curious.
Why is it necessary to also recompile the definitions that use a Common Lisp macro when we modify it (besides, obviously, recompile the modified Common ...
8
votes
3
answers
760
views
Why is the @ sign needed in this macro definition?
In the following when macro:
(defmacro when (condition &rest body)
`(if ,condition (progn ,@body)))
Why is there an "at" @ sign?
8
votes
2
answers
161
views
Is there an advantage to this macro?
I am reading Practical Common Lisp by Peter Seibel. In Chapter 9, he is walking the reader through creating a unit testing framework, and he includes the following macro to determine whether a list is ...
8
votes
2
answers
2k
views
Difference between a macro definition and function definition
I'm trying to learn Lisp but I got stuck by this example (you can find it on "ANSI Common Lisp", by Paul Graham, page 170):
(defmacro in (obj &rest choices)
(let ((insym (gensym)))
`(...
7
votes
2
answers
265
views
Avoiding the pitfall of using anaphoric macro unwittingly
How do I know whether I'm calling an anaphoric macro? If I do so without knowing it, some seemingly unbound symbols might behave quite different from what one would expect.
Example
Collecting all ...
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
270
views
How to modify place with arbitrary function
Sometimes we need to modify a place but here is no built-in function that meets our needs.
For instance, here are incf and decf for addition and subtraction:
CL-USER> (defvar *x* 5)
*X*
CL-USER&...
7
votes
3
answers
599
views
Catch-22 situation with Common Lisp macros
Often when I try to write a macro, I run up against the following difficulty: I need one form that is passed to the macro to be evaluated before being processed by a helper function that is invoked ...
7
votes
1
answer
251
views
Understanding loop macro expansion
I expanded the macro below to see how it worked and found myself a little confused.
(loop for i below 4 collect i)
expands to (I have cleaned it up a little for readability)
(block nil
(let ((i 0)...
7
votes
4
answers
1k
views
defmacro with defclass
I have a class in Common Lisp:
(defclass my-cool-class()
((variable1
:initarg :variable1
:accessor variable1
:initform (error "Must supply value to variable1"))
(variable2
:...
7
votes
1
answer
730
views
What is a macro-function?
I was playing around with macros today and saw the term macro-function appear in the REPL. I am familiar with Macros, compiler macros and reader macros but have not run into these.
CL-USER> (...
7
votes
1
answer
220
views
Does Clojure's single-namespace approach constrains you in any way when programming macros?
In the article Technical Issues of Separation in Function Cells and Value Cells, Kent Pitman and Richard Gabriel explains the decision of making Common Lisp a Lisp-2:
There are two ways to look at ...
6
votes
8
answers
3k
views
Writing a ++ macro in Common Lisp
I've been attempting to write a Lisp macro that would perfom the equivalent of ++ in other programming languages for semantic reasons. I've attempted to do this in several different ways, but none of ...
6
votes
3
answers
2k
views
How to define symbols that will work like ( and ) by symbol macro?
I am trying define symbols a and b in following way
a + 1 1 b
2
I am trying to do this by using define-symbol-macro
(define-symbol-macro a '( )
(define-symbol-macro b ') )
but this way is not ...
6
votes
2
answers
540
views
Why does this Lisp macro as a whole work, even though each piece doesn't work?
I'm reading/working through Practical Common Lisp. I'm on the chapter about building a test framework in Lisp.
I have the function "test-+" implemented as below, and it works:
(defun test-+ ()
(...
6
votes
1
answer
6k
views
Zipping lists together in Common Lisp - Problem with "and"
What I am trying to do is create a function zip (note now that this is not homework) that iterates through multiple lists simultaneously, applying a function to each list of elements, like so:
(zip f ...
6
votes
6
answers
2k
views
Is there a simple example for explaining Lisp macros to a "generic" programmer?
I was having a conversation with a colleague recently and tried telling him about the beauty of (Common) Lisp. I tried to explain macros somehow, since I consider macros one of the killer features of ...
6
votes
1
answer
307
views
Can macro expansion contain (declare ...) expressions?
The Common Lisp Hyperspec states "Macro forms cannot expand into declarations; declare expressions must appear as actual subexpressions of the form to which they refer."
I'm confused on the meaning ...
6
votes
6
answers
2k
views
How to write a recursive macro call on a &REST parameter in Lisp?
I've been writing some simple test cases for one of my assignments, and have built up a bit of a test suite using macros. I have run-test and run-test-section and so on.
I'd like run-test-section to ...
6
votes
2
answers
2k
views
What is the purpose of the `intern` function?
I'm following an article where the author defines the following macro:
(defmacro make-is-integral-multiple-of (n)
(let ((function-name (intern (concatenate
'string
...
6
votes
4
answers
449
views
In Common Lisp, how to define a macro expanding to nothing (rather than `nil`)?
I want to define a macro which can comment a s-expression, for example:
I hope that
(list 1 2 (comment-macro (something))) -> (1 2)
But if I define the macro like this
(defmacro comment-macro (...
6
votes
1
answer
231
views
Non-recursive symbol-macrolet
I'd like to expand all symbols x in a certain fragment into (value x). E.g.
(lambda ()
(* x x))
should become
(lambda ()
(* (value x) (value x)))
A simple use of symbol-macrolet wouldn't work, ...
6
votes
1
answer
441
views
How to check if a symbol is a macro
In common lisp, I would like to be able to find out wether or not a symbol is a macro or not. Is there a predicate such as (macrop) which will allow me to detect if a name/symbol is a macro?