2

Arguments will be evaluated during a function call in Lisp. Is there any way besides macro to print the argument without evaluation?

Take an example in Common Lisp:

(defun foo (&rest forms)
  (loop for i in forms collect i))

Call "foo" in REPL toplevel:

CL-USER> (foo (= 1 2) (< 2 3))

Got the result:

(NIL T)

Is there any way to get this result?:

((= 1 2) (< 2 3))
Will Ness
  • 70,110
  • 9
  • 98
  • 181
jarod.tian
  • 31
  • 5
  • You'll almost certainly need a macro for that. The only exception I can think of is that the special variables *, /, and + let you special stiff in the REPL, like currently evaluating form. If you only need this in the REPL, you could use something with those. – Joshua Taylor Dec 24 '15 at 02:45
  • Thanks Joshua! I just want to confirm my understanding of function and macro. Actually I want the "code" to be the "data". That's what macro is good at. – jarod.tian Dec 24 '15 at 02:54

2 Answers2

8

You can’t do that in either Scheme or Common Lisp without macros. Of course, it’s also pretty trivial with macros, so feel free to use them if they fit your use case.

That said, there’s a bit more to this question than you may have anticipated. You’re effectively asking for a feature that was present in older Lisps that has fallen out of fashion, known as fexprs. A fexpr is exactly what you describe: a function whose operands are passed to it without being evaluated.

Most modern dialects have done away with fexprs in favor of only using macros, and you can see this Stack Overflow question for more information on why. The gist is that fexprs are hard to optimize, difficult to reason about, and generally less powerful than macros, so they were deemed both redundant and actively harmful and were summarily removed.

Some modern Lisps still support fexprs or something like them, but those dialects are rare and uncommon in comparison to the relative giants that are Scheme and CL, which dominate the modern Lisp world. If you need this sort of thing, just use macros. Better yet, just quote the arguments so you don’t need any macros at all. You’ll be more explicit (and therefore much clearer), and you’ll get the same behavior.

Alexis King
  • 43,109
  • 15
  • 131
  • 205
  • As far as I remember, fexprs where an experimental side track in a few dialects, but never something you'd generally expect from a Lisp. – Svante Dec 25 '15 at 00:40
  • Thanks Alexis! Your answer opened my eye a lot. Actually I had this question when I was reading the book . I was thinking why I cannot use plain function rather than macro. That means why macro is necessary. So I tried to simulate macro's behaviour by writing the function "foo" in this question. Maybe I need more thinking about "macro" to fully understand it's secret. I will read the reference you quoted. – jarod.tian Dec 27 '15 at 02:28
2

Yes; you can get the result with an operator called quote, if you don't mind one more level of nesting:

(quote ((= 1 2) (< 2 3)))
-> ((1 2) (2 3))

quote isn't a macro; it is a special operator.

Kaz
  • 55,781
  • 9
  • 100
  • 149