[plt-scheme] Re: Interaction of shared and first, second, etc. (bug?)

From: David Van Horn (dvanhorn at cs.brandeis.edu)
Date: Tue Jul 29 01:47:34 EDT 2008



Gregory Woodhouse wrote:
> I was under the impression that first was a synonym for car, second for 
> cadr, etc. But apparently,, this isn't so with shared variables

Car operates on pairs, while first operates only on lists.  This is 
clear from the contracts and documentation:

    (car p) -> any/c

      p : pair?

    Returns the first element of the pair p.

    (first lst) -> any/c

      lst : list?

    The same as (car lst), but only for lists (that are not empty).

Now compare the definitions of pair and list:

    A pair combines exactly two values. The first value is accessed with
    the car procedure, and the second value is accessed with the cdr
    procedure. Pairs are not mutable (but see Mutable Pairs and Lists).

    A list is recursively defined: it is either the constant null, or it
    is a pair whose second value is a list.

Revisiting your example:

    (shared ((a (cons 1 a))) a)

This produces a value that is not a list, but is a pair (whose car is 1, 
and whose cdr is the pair itself).  So you may apply car, but not first.

An analogous thing happens with (first (cons 1 2)) vs (car (cons 1 2)).

David





Posted on the users mailing list.