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

constants in Scheme



   Date: Thu, 8 Mar 90 17:35:30 +0100
   From: Oliver Laumann <net%TUB.BITNET@mitvma.mit.edu>

   I still don't quite understand which objects are considered immutable in
   Scheme.  The standard says "The constants and the strings returned by
   symbol->string are then the immutable objects, while all objects created
   by the other procedures in this Standard are mutable".

The basic rule is that any object that appears in your program is
immutable.  Thus quoted lists, quoted vectors, and any strings that
appear in the text of your program are immutable.

In addition, the strings returned by symbol->string are also
immutable.

The standard does not define any other immutable objects.

   Does this mean that objects returned by, say, "read" are mutable?

Yes.

   Is the expression

       (string-set! (read) 0 #\a))

   legal, when the call to (read) returns a non-empty string?

Yes.

   And what about
   the elements of a vector constant?  Is

      (set-car! (vector-ref '#((a b) c) 0) 'd)

   legal?

This is not legal.  Both the vector, and its elements (just like the
elements of a quoted list) are objects that textually appear in your
program, and thus are immutable.

   By the way, why must vector constants be quoted?  Does the evaluation
   of a vector constant ever return a result?

Unquoted vectors are not defined as expressions; a program containing
an unquoted vector in its text is what the standard calls a
"non-conforming program".  I don't know any good reason why these are
illegal.