Question

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.)

Était-ce utile?

La solution

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".

Autres conseils

`(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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top