不可变的 lisp 对象
Immutable lisp objects
Peter Seibel 在 Practical Common Lisp Ch 6. Variables, footnote 4, mentions that integers and characters are immutable. (To this short list, sds at 中添加了路径名。)
这是否意味着像 complex
、ratio
和 float
这样的非整数类型实际上是可变对象?也就是说,有可以破坏性修改的组件吗?例如,是否可以更改复数的 real-part
? (但是,通常的方法——例如,(setf (real-part cplx) 2)
不起作用。)或者复数可能是一个不可变的对象,但具有某些 integer
不共享的特殊属性?
此外,在各种 copy-
函数(以及像 remove
这样复制其参数的其他函数)期间不复制存在于更复杂的可变对象中的不可变对象是否正确?并且只复制封闭的可变对象提供的结构,同时保留相同的不可变对象?
您在问:
... is a complex number an immutable object, but with some special properties not shared by integer?
这是真的,因为在您引用的注释中,在讨论直接表示与通过指针表示时讨论了整数和字符的不变性:
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)
其实不同的其他对象是不可变的,像字符、所有数字、符号、路径名、函数等,但它们通常通过指针来表示(这就是“特殊的属性”),因此注释中的讨论不适用于它们。
关于你的最后一个问题,关于复制运算符,语义取决于特定的运算符(例如是否参见 copy-seq
), for instance if the container copied contains the same 元素,并且与它们的可变性或不可变性无关。
Peter Seibel 在 Practical Common Lisp Ch 6. Variables, footnote 4, mentions that integers and characters are immutable. (To this short list, sds at
这是否意味着像 complex
、ratio
和 float
这样的非整数类型实际上是可变对象?也就是说,有可以破坏性修改的组件吗?例如,是否可以更改复数的 real-part
? (但是,通常的方法——例如,(setf (real-part cplx) 2)
不起作用。)或者复数可能是一个不可变的对象,但具有某些 integer
不共享的特殊属性?
此外,在各种 copy-
函数(以及像 remove
这样复制其参数的其他函数)期间不复制存在于更复杂的可变对象中的不可变对象是否正确?并且只复制封闭的可变对象提供的结构,同时保留相同的不可变对象?
您在问:
... is a complex number an immutable object, but with some special properties not shared by integer?
这是真的,因为在您引用的注释中,在讨论直接表示与通过指针表示时讨论了整数和字符的不变性:
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)
其实不同的其他对象是不可变的,像字符、所有数字、符号、路径名、函数等,但它们通常通过指针来表示(这就是“特殊的属性”),因此注释中的讨论不适用于它们。
关于你的最后一个问题,关于复制运算符,语义取决于特定的运算符(例如是否参见 copy-seq
), for instance if the container copied contains the same 元素,并且与它们的可变性或不可变性无关。