1

The cond-expression in Scheme is a special form, but is the else keyword, used as final case in a cond-expression, a special form? Or is it just a reserved keyword that is essentially equivalent to the truth-value #t ? In the latter case, why cannot I write something like (?eq else #t)?

Kim Mens
  • 325
  • 1
  • 13

3 Answers3

3

It's part of the syntax of cond and case. R7RS specifies the following syntax:

(cond <cond clause>+)
(cond <cond clause>* (else <tail sequence>))

(case <expression>
  <case clause>+)
(case <expression>
  <case clause>*
  (else <tail sequence>))

It's not defined outside the syntax of these special forms.

Barmar
  • 741,623
  • 53
  • 500
  • 612
2

The Scheme standards call else, as used in a cond form, auxiliary syntax. R6RS shows one possible implementation of cond using syntax-rules; here else is called a <literal>:

(define-syntax cond
  (syntax-rules (else =>)
    ((cond (else result1 result2 ...))
     (begin result1 result2 ...))
;; ...

Note that else is not a replacement for #t. A <literal> is an identifier that is used to match input subforms; it is treated as a syntactic keyword within the syntax-rules form.

ad absurdum
  • 19,498
  • 5
  • 37
  • 60
1

Neither. You can test this by evaluating else outside of a cond: it behaves just like any other unbound symbol. The cond macro treats it specially, but there is nothing inherently special about it in any other context.

amalloy
  • 89,153
  • 8
  • 140
  • 205
  • It doesn't behave as an unbound symbol. When using an unbound symbol I would get something like ``cannot reference undefined identifier else'' but instead when using else in another context than the last cond case I get this error instead: ```else not allowed as an expression```. So I guess it is syntactically imposed to be only used in the context of an else, even though semantically it can be seen as a #t true value. – Kim Mens Jan 21 '21 at 16:52
  • @KimMens I don't think that's required by the language specification, it sounds like an extra sanity check by your implementation. – Barmar Jan 21 '21 at 16:59