exercises and notes from "Gentle introduction"
This commit is contained in:
48
assignment.lisp
Normal file
48
assignment.lisp
Normal file
@@ -0,0 +1,48 @@
|
||||
|
||||
(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
|
||||
Reference in New Issue
Block a user