2

How is it, that in

`(1 ,(+ 1 1) (- 4 1) 4) ; '(1 2 (- 4 1) 4)

the minus sign ("-") is not treated as an operator (but as a symbol; '- instead of #'- - correct?) (This part I think I understand.)

But why is it, that the third left parenthesis is indeed evaluated to '( -> (list ... (That is, a list/expression delimiter and not just a literal like the '- above?) Does the interpreter "peek ahead" for the closing delimiter or does it simply say, "OK, this should be a list. If there is no delimiter to the right the expression is not valid and that's not my problem."?

Sorry for a confusing question; to boil it down, I guess my question is: how does the interpreter step by step evaluate the above list correctly? (Also feel free to correct terminology.)

Emanuel Berg
  • 499
  • 4
  • 14

2 Answers2

4
`(1 ,(+ 1 1) (- 4 1) 4)

Backquote is a read macro. It transforms the expression at READ TIME.

Do this:

 (read-from-string "`(1 ,(+ 1 1) (- 4 1) 4)")

This gets read as an implementation specific form. Something similar to this:

 (list* 1 (+ 1 1) '((- 4 1) 4))

The CL standard does not specify what the backquote parses to.

So above transformation is done by the READER.

Evaluation then is done using the usual rules. Nothing special.

LIST* takes the first args and conses them before the last arg, which is a list.

Rainer Joswig
  • 136,269
  • 10
  • 221
  • 346
  • When does read time occur? About CL, I've only heard about compile time and run time (the latter in interpretative mode) - and this was in the context of macro expansion, I should say. – Emanuel Berg Jul 22 '12 at 21:02
  • @Emanual Berg: 'READ TIME' is when READ runs. When textual s-expressions gets read into Lisp data. – Rainer Joswig Jul 22 '12 at 21:08
  • You write an expression, then hit enter, the interpreter reads it - that's when? And, you `load` a file, then one line at a time, read, that line is evaluated, read the next line, ... ? – Emanuel Berg Jul 22 '12 at 21:12
  • @Emanuel Berg: The 'Interpreter' reads nothing. An 'Interpreter' interprets. READ reads things. See the documentation of the function READ. READ also runs the read macros. – Rainer Joswig Jul 22 '12 at 21:13
  • I read documentation and tutorials all day long - but I think you can learn a lot in a social context. I say this because you sound actually annoyed with my questions. If this is in fact not so, my mistake. Sure, will look up READ. – Emanuel Berg Jul 22 '12 at 21:19
2

i'm trying to imagine what you are thinking that is causing the confusion. i guess that the problem is:

if backquote quotes things, why do parentheses still mean lists, rather than just being one more piece of text?

if that is what you are asking, then the answer (roughly - people like rainer know a lot more about lisp than me) that quoting isn't as simple as you think. when the code is read by lisp, it is processed by a thing called "the reader". that turns the code into a syntax tree - a bunch of lists that form a tree that contains the program.

quoting is just an instruction to the reader that says something like:

treat `(a ,b) as (list 'a b)

and comma works something like

ignore the above - do what you normally do

i don't know if that helps. if i am contradicting rainer then he (i assume it's a male name?) wins. i am just trying to get more inside your head.

oh - one more thing. quoting doesn't make things "text". it makes words atoms (and brackets lists). so it's really not as simple as "make this text".

andrew cooke
  • 45,717
  • 10
  • 93
  • 143
  • "if backquote quotes things, why do parentheses still mean lists, rather than just being one more piece of text?" That is exactly the point, save that I knew that it wasn't text. (I wrote "symbol", but yours "word atom" is probably better.) As for you answer - is it fair to say - **first** the tree is done according to the lists, **then** operations are carried out, and backquote is a type of operator, that's why a list within a backquote is still a list? (Sill some confusion.) – Emanuel Berg Jul 22 '12 at 20:54
  • this is applied by the reader. it's done at the *same time* as converting to lists. backquote is not an operator in the same sense as anything that happens at runtime. it's a special instruction to the code that parses input. it happens before anything is evaluated. – andrew cooke Jul 22 '12 at 21:13