1

I'm going through this Scheme tutorial and in section 3 (Making Lists) the guy says you should write '() to represent an empty list. But for every test I've wrote seems that it has the very same effect as using just ().

Also, as far as I understand, the quote means the interpreter won't evaluate the expression, but seems that the interpreter knows what's after the ' symbol, because doing this (cons 1 '()) yields (1), while doing this (cons 1 'abc) yields (1 . abc), so it knows '() is an empty list but 'abc is not.

1
  • 2
    Both Bigloo and Racket throw an error if the empty list is not quoted.
    – uselpa
    Oct 19, 2013 at 16:15

2 Answers 2

5

Some Scheme implementations permit bare () as a synonym for '(), but only the quoted form is standard.

As for your second question: consider

(define abc '(1 2 3))
(define def '(1 2 3))

(cons 0 'abc)
(cons 0 'def)
(cons 0 abc)
(cons 0 def)

In the first two expressions, abc and def are not evaluated, so they stay symbols. In the latter two, they are evaluated to the objects they stand for, which are both equal to the list (1 2 3).

2
  • 2
    I think I get it know. The quote doesn't ignore what is after, but takes it as is, so '() means an empty list as is, which is why it knows is an empty list, and an unquoted list tells the interpreter to call the first item and pass the next items as parameters, which is why () should really be an error, while allowed by some interpreters.
    – Juan
    Oct 19, 2013 at 16:39
  • @JuanLuisSoldi: indeed. Allowing () would complicate the language definition because it requires an extra rule.
    – Fred Foo
    Oct 19, 2013 at 16:41
1

TL;DR: To make sure your applications work as as designed you should quote the empty list since it's unsure if it will work otherwise. see the long answer below.

As for how Scheme works for quoted values, quoting '(+ 3 4 5) makes an expression a constant that is not to be evaluated. It much like making a string with code in it, like "if( a == 0 ) return 4;" in Java or C. The difference is that a quoted expression are structured data rather than byte sequences.

(cons 1 'abc) and (cons 1 '()) does the same. A cons has two placeholders for values and those two expressions sets two values in the exact same manner. It's only display (and the repl) that knows that a list that ends with () should display differently and not (1 . ()) like it actually is stored.

The long answer about the need to quote the empty list

It all boils down to the standard you're using. Most implementations today are R5RS and it requires the empty list be quoted since the empty list is not an expression. Implementations might still allow it though since it won't interfere with a proper Scheme application. Heres a quote from the R5RS report:

Note: In many dialects of Lisp, the empty combination, (), is a legitimate expression. In Scheme, combinations must have at least one subexpression, so () is not a syntactically valid expression.

This actually happened in R3RS (under Procedure calls) so it's been around for a while. When looking for it in R6RS however it seems to have disappeared from the section making me think they have reverted it so that it would be self evaluating. However, I cannot find it in the language changes part.

When looking at the R7RS draft (NB: PDF), the part from R5RS is back so I guess this was an error in the R6RS report. This might be the reason racket (and probably other implementors) allow () as an expression in R6RS to be sure it will work even when the report is ambiguous about it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.