common-lisp-study/assignment.lisp

49 lines
1.0 KiB
Common Lisp

(setf circ (list 'foo))
(setf (cdr circ) circ)
circ
;;; setf modifies "pointers"
;;; and getter returns a reference
;;; so using setf with getter as "place" works as a setter
;;;
;;; append - nondestructive
;;; nconc - destructive merging of lists
(nconc '(hello lists) '("and" "with" other 15 "values"))
;; subst - nondesctructive
;; nsubst - destructive
(setf tree '(i say (e i (e i) o)))
(subst 'a 'e tree)
tree
(nsubst 'a 'e tree)
(nsubst 'cherry '(a i) tree :test #'equal)
;; this modifies the list
;;; exercise,
;; chop - redule non nil list to list of head
(defun chop (x)
(setf (cdr x) nil))
(setf my-list '(1 2 "hello"))
(chop my-list)
my-list
(defun ntack (x a)
(setf (cdr (last x)) (list a)))
(setf my-test-ntack-list '(1 2 "hello"))
(ntack my-test-ntack-list 'boo)
my-test-ntack-list
;; and more recursion
(setf h '(hi ho))
(setf (cdr (last h)) h)
;;; setq - before "generalized variables", only sets value of ordinary variables
(setq x '(slings and arrows))
(setq (cdr x) (list "hello")) ; variable name is not a symbol