chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Chicken-users] Syntax of case expressions


From: Tobia Conforto
Subject: Re: [Chicken-users] Syntax of case expressions
Date: Mon, 3 Mar 2008 01:11:22 +0100

Elf wrote:
(define foo 'quote)
(case foo
  ('a 1)
  (else 2)) => 1

the proper behaviour in this case, btw, has not yet been given.

This *is* the proper behaviour, although it could qualify for an "Obfuscated Scheme" contest.

You see, case is a macro. It parses its body with its own rules. One of these is that each clause must be a list that begins with either 1. a sublist of one or more values, or 2. the symbol else. Everything else is an error.

'a is not a proper s-expression. It's a reader shorthand for (quote a). Therefore the reader (the part of Chicken that translates syntactic shorthands into proper s-expressions) reads your example above as:

(case foo
  ((quote a) 1)
  (else 2))

The trick here is that (quote a) is not evaluated under normal rules: Chicken does not execute the special form quote, which would quote the symbol a. Instead (quote a) is parsed by case under its own rules: as a list of values to match upon. It so happens that (quote a) is a list of two values, the symbol quote and the symbol a. The value of foo is one of those values, so case considers the first clause matched and proceeds to return 1.


HTH
Tobia

PS: we need an Obfuscated Scheme Contest




reply via email to

[Prev in Thread] Current Thread [Next in Thread]