Search code examples
schemelispevaluationquote

Why does '(1 2 3) evaluate to a list en Lisp (Scheme)?


The quote (') is used to introduce a pre-evaluated value, so (quote x) results in the symbol x and not what the symbol evalutes to.

Numbers, Booleans, characters and strings are self-evaluating in Scheme, so quoting them doesn't matter.

But why does (quote (1 2 3)) or (quote ()) answers #t to the predicate list?.

Should't the result be a "pre-evaluated" value? But in this case (1 2 3) has actually been evaluated to (list 1 2 3)?

Thank you.


Solution

  • pre-evaluated value

    I'm not sure where you got that term from. I've never used it. It's not "pre-evaluated", it's unevaluated.

    This is really all works from the fact Lisp (and Scheme) is Homoiconic: the structure of the program really uses lists and atoms underneath.

    quote is the dual to eval: (eval (list '+ '1 '2 '3)) (and since a quoted number is just the number, (eval (list '+ 1 2 3)) does it as well) is the opposite of (quote '(+ 1 2 3)).

    An evaluated list is a call, so an unevaluated call is a list.

    Should't the result be a "pre-evaluated" value? But in this case (1 2 3) has actually been evaluated to (list? 1 2 3)?

    You're missing some parentheses here! You get (list? '(1 2 3)) (or (list? (quote (1 2 3)). That is, (list? (list 1 2 3)). Which is true.

    You can check the opposite with (eval (list '+ 1 2 3)): you get 6.

    Note: Some values just evaluate to themselves (like numbers or functions. You can throw eval at it as many times as you want, and it won't change a thing: (eval (eval (eval 1))) is just 1.)