All Questions

Tagged with
Filter by
Sorted by
Tagged with
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 ...
minty's user avatar
  • 22.3k
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: ...
unj2's user avatar
  • 52.8k
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 ...
Mike Stone's user avatar
  • 44.5k
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 ...
Louis's user avatar
  • 2,462
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: ...
Joshua Fox's user avatar
  • 19.1k
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. ...
Dimitri C.'s user avatar
  • 22.2k
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 ...
dreeves's user avatar
  • 26.7k
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 ...
Jay's user avatar
  • 9,586
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 ...
Jason Baker's user avatar
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 ...
hawkeye's user avatar
  • 35.2k
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 ...
apg's user avatar
  • 2,641
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 ...
Rick Copeland's user avatar
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))) ...
Steven Devijver's user avatar
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 ...
d11wtq's user avatar
  • 35k
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 ...
Chris Taylor's user avatar
  • 47.2k
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?
Pedro Rolo's user avatar
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 ...)` '(...
adrusi's user avatar
  • 845
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 ...
Tim Gilbert's user avatar
  • 5,811
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 ...
mannicken's user avatar
  • 6,983
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)...
Racket Noob's user avatar
  • 1,056
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 ...
fogus's user avatar
  • 6,166
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 ...
porky11's user avatar
  • 604
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 ...
Marcus Junius Brutus's user avatar
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 ...
utxeee's user avatar
  • 963
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-...
Nick Orton's user avatar
  • 3,623
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 ...
Jason Baker's user avatar
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)) ...
Mike Manilone's user avatar
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'...
JoeCamel's user avatar
  • 722
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. (...
Greg Nisbet's user avatar
  • 6,850
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 (...
steglig's user avatar
  • 1,128
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 ...
Steven Huwig's user avatar
  • 20.4k
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 ...
nil's user avatar
  • 161
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 ...
ttsiodras's user avatar
  • 10.9k
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 ...
hawkeye's user avatar
  • 35.2k
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 ...
Kyle Burton's user avatar
  • 27.1k
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 ...
Sean Woods's user avatar
  • 2,514
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, ...
jtmoulia's user avatar
  • 660
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?
Le Curious's user avatar
  • 1,461
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?
Ertuğrul Çetin's user avatar
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-...
kes's user avatar
  • 359
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 ...
Tuomas Toivonen's user avatar
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 ...
krzysz00's user avatar
  • 2,083
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.
Denis Nutiu's user avatar
  • 1,327
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 ...
greenoldman's user avatar
  • 20.5k
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 ...
Hanfei Sun's user avatar
  • 46.2k
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 (...
snowape's user avatar
  • 1,294
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, ...
sadakatsu's user avatar
  • 1,275
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 ...
Bogatyr's user avatar
  • 19.3k
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 ...
Paulo Tomé's user avatar
  • 1,940
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 ...
sdasdadas's user avatar
  • 24.4k

1
2 3 4 5
7