All Questions
335
questions
360
votes
15
answers
78k
views
What makes Lisp macros so special?
Reading Paul Graham's essays on programming languages one would think that Lisp macros are the only way to go. As a busy developer, working on other platforms, I have not had the privilege of using ...
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
14
answers
8k
views
How far can LISP macros go? [closed]
I have read a lot that LISP can redefine syntax on the fly, presumably with macros. I am curious how far does this actually go? Can you redefine the language structure so much that it borderline ...
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 ...
37
votes
8
answers
9k
views
What can you do with Lisp macros that you can't do with first-class functions?
I think I understand Lisp macros and their role in the compilation phase.
But in Python, you can pass a function into another function
def f(filename, g):
try:
...
35
votes
8
answers
4k
views
Examples of what Lisp's macros can be used for
I've heard that Lisp's macro system is very powerful. However, I find it difficult to find some practical examples of what they can be used for; things that would be difficult to achieve without them.
...
34
votes
1
answer
3k
views
Mathematica: Unevaluated vs Defer vs Hold vs HoldForm vs HoldAllComplete vs etc etc
I'm bewildered by all the built-in Mathematica functions that purport to prevent evaluation in some way: Unevaluated, Defer, Hold, and over half a dozen of the form Hold*. The Mathematica ...
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 ...
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
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
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 ...
21
votes
4
answers
5k
views
How to implement a Lisp macro system?
I've implemented my own Lisp on top of node.js, I can run s-expressions like this:
(assert (= 3 (+ 1 2)))
(def even? (fn [n] (= 0 (bit-and n 1))))
(assert (even? 4))
(assert (= false (even? 5)))
...
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
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 ...)`
'(...
19
votes
1
answer
4k
views
Help me write a Clojure macro which automatically adds metadata to a function definition
I realize that the first rule of Macro Club is Don't Use Macros, so the following question is intended more as an exercise in learning Clojure than anything else (I realize this isn't necessarily the ...
18
votes
4
answers
4k
views
(emulated) Macros in Haskell?
A person on Reddit has brought this code to my attention:
main = do
let ns = [print 1, print 2, print 3]
sequence_ ns
sequence_ $ reverse ns
sequence_ $ tail ns ++ [head ns]
head ns
What's ...
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)...
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
4
answers
3k
views
F# Type Providers vs. Lisp macros
I've been reading about F# 3.0 type providers (e.g. here) and it seems that they are based on a kind of compile-time code generation. In that respect I was wondering how they compare against Lisp ...
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 ...
15
votes
1
answer
3k
views
How do I create a macro to define two functions in clojure
The code below doesn't behave as I would expect.
; given a function name, its args and body, create 2 versions:
; i.e., (double-it foo []) should create 2 functions: foo and foo*
(defmacro double-...
14
votes
5
answers
2k
views
Why does let require a vector?
I never really thought about this until I was explaining some clojure code to a coworker who wasn't familiar with clojure. I was explaining let to him when he asked why you use a vector to declare ...
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
2
answers
2k
views
Apply-recur macro in Clojure
I'm not very familiar with Clojure/Lisp macros. I would like to write apply-recur macro which would have same meaning as (apply recur ...)
I guess there is no real need for such macro but I think it'...
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.
(...
14
votes
3
answers
1k
views
Emacs: getting readable keyboard-macros
When using insert-kbd-macro to save a named keyboard macro I get "unreadable" Lisp code like
(fset 'ppsql
(lambda (&optional arg) "Keyboard macro." (interactive "p") (kmacro-exec-ring-item (...
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
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
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 ...
11
votes
6
answers
723
views
How does a macro-enabled language keep track of the source code for debugging?
This is a more theoretical question about macros (I think). I know macros take source code and produce object code without evaluating it, enabling programmers to create more versatile syntactic ...
11
votes
1
answer
704
views
Is there a clean way to add functions to a dynamically created namespace?
I am creating a noir webapp, and I need to dynamically create new views and models. I've been following the noir examples, in which the view and the controller for a resource have separate namespaces, ...
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
1k
views
What is the difference between macroexpand and macroexpand-1 in Clojure
I couldn't understand the difference between macroexpand and macroexpand-1.
Could you provide examples?
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
1
answer
879
views
Clojure, can macros do something that couldn't be done with a function
I'm learning Clojure macros, and wonder why we can't use just functions for metaprogramming.
As far as I know the difference between macro and function is that arguments of macro are not evaluated ...
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
5
answers
1k
views
Is homoiconicity really needed for having macros?
2012-02-04 is sponsored by word "homoiconicity" http://en.wikipedia.org/wiki/Homoiconicity .
Background: I am about to choose which book about Clojure to buy -- either "Clojure in ...
9
votes
3
answers
3k
views
In Clojure, what does `&` mean in function/macro signature?
I saw the usage of & in Clojure function signature like this (http://clojure.github.io/core.async/#clojure.core.async/thread):
(thread & body)
And this:
(doseq seq-exprs & body)
Does ...
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
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
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
4
answers
953
views
Is it possible to build Lisp-like macros into an imperative language?
What is preventing a language like C from having Lisp macros? At what point in the compilation process does C forego the ability to manipulate its code tree?
And, is this specifically an interpreted ...