Is all data in Lisp immutable?

In this article, the author praised functional programming as two key good features. But he did not mention (Common) Lisp.

Does Lisp data match "all data immutable" data?

+3
source share
3 answers

In Common Lisp you can use a functional style. Avoid setf, setqetc., and you have a functional programming language. In other words, do not change the values โ€‹โ€‹of variables, do not change the contents of composite data structures (cons, vectors, structures, etc.) after creation. Functions take input and produce output without changing state.

, Common Lisp , , , .

+6

Common Lisp ( , , -) , - , .

, , , CLOS, . , :

(defstruct person
  (name nil :type string :read-only t)
  (age nil :type (integer 0 100)))

(let ((john (make-person :name "John" :age 30)))
  (princ john)
  ;; * `age' is mutable:
  (incf (person-age john))
  (princ john)
  ;; * `name' is not:
  ;; (setf (person-name john) "garbage name")
  ;; ^ you can't do this because the `defstruct' macro just don't emit SETFer
  ;;   for the `name' slot as you made it read-only.
  )

( const C, , Common Lisp , , ), , , .


. :

+6

No. Data in Common Lisp is not immutable.

as shown in SBCL using function setf

* (setf x 0)
0
* x
0
* (setf x 1)
1
* x
1
0
source

All Articles