davypough
davypough

Reputation: 1941

Immutable lisp objects

Peter Seibel in Practical Common Lisp Ch 6. Variables, footnote 4, mentions that integers and characters are immutable. (To this short list, sds at Nondestructive setf? adds pathnames.)

Does this mean that non-integer number types like complex, ratio, and float are actually mutable objects; that is, have components that can be destructively modified? For example, is it possible to change the real-part of a complex number? (However, the usual method--e.g., (setf (real-part cplx) 2) does not work.) Or perhaps is a complex number an immutable object, but with some special properties not shared by integer?

Also, is it correct to say that the immutable objects present in more complex mutable objects are not copied during the various copy- functions (and other functions like remove that copy their arguments); and that only the structure provided by the enclosing mutable objects is copied, while retaining the identical immutable objects?

Upvotes: 0

Views: 442

Answers (1)

Renzo
Renzo

Reputation: 27424

You are asking:

... is a complex number an immutable object, but with some special properties not shared by integer?

This is true, since in the note that you have cited, the immutability of integers and characters is discussed when talking about direct representation vs. representation through a pointer:

As an optimization certain kinds of objects, such as integers below a certain size and characters, may be represented directly in memory where other objects would be represented by a pointer to the actual object. (emphasis is mine)

In fact different other objects are immutable, like characters, all numbers, symbols, pathnames, functions, etc., but they are usually represented through pointers (and this is the “special property”), so that the discussion in the note does not apply to them.

For your last question, about copying operators, the semantics depends on the specific operator (see for instance copy-seq), for instance if the container copied contains the same elements or not, and it is not related to their mutability or immutability.

Upvotes: 2

Related Questions