[plt-scheme] Syntax question (from 4.2.1 of Report)

From: Robby Findler (robby at cs.uchicago.edu)
Date: Tue Jan 3 08:47:37 EST 2006

At Tue, 3 Jan 2006 05:40:57 -0800, Gregory Woodhouse wrote:
> I'm puzzled by this example from the report
> 
> (cond ((assv 'b '((a 1) (b 2))) => cadr)
>        (else #f))
> 
> which evaluates to 2. I understand that (assv 'b '((a 1) (b 2)))  
> evaluates to (b 2), and cadr is an abbreviation for the car of the  
> cdr, but don't quite know what to make of the cond expression.

=> is treated specially by cond expressions. When a cond clause has
three pieces and the second one is =>, then the third is expected to be
a procedure and the result of the cond-test for that line is applied to
that procedure.

Written as rewriting rules:

  (cond [#f e] clauses ...) => (cond clauses)
  (cond [v e] clauses ...) => e   ;; v not #f
  (cond [v => e] clauses ...) => (e v)  ;; v not #f
  (cond) => (void)

where the only sub-expression in a cond that is evaluated is the first
question part.

(cond is slightly more complex than that because of the implicit begin
in the first two cases, but hopefully you get the idea)

hth,
Robby


Posted on the users mailing list.