Circular list in Common Lisp -


i working using visual programming environment musical composition based on cl . trying create function when given 3 elements (1 2 3) return 1, 2, 3, 1, 2, 3 etc., 1 number @ time each time evaluated. book common lisp gentle introduction, mentions briefly it's possible create circular lists using sharp-equal notation not details on how use them. keep in mind can insert actual lisp code in program using object designed that.

cl-user 3 > (defun circular (items)               (setf (cdr (last items)) items)               items) circular  cl-user 4 > (setf *print-circle* t) t  cl-user 5 > (circular (list 1 2 3)) #1=(1 2 3 . #1#) 

example:

cl-user 16 > (setf c1 (circular (list 1 2 3))) #1=(1 2 3 . #1#)  cl-user 17 > (pop c1) 1  cl-user 18 > (pop c1) 2  cl-user 19 > (pop c1) 3  cl-user 20 > (pop c1) 1 

also:

cl-user 6 > '#1=(1 2 3 . #1#) #1=(1 2 3 . #1#) 

with bit of clos added:

(defclass circular ()   ((items :initarg :items)))  (defmethod initialize-instance :after ((c circular) &rest initargs)   (setf (slot-value c 'items) (circular (slot-value c 'items))))  (defmethod next-item ((c circular))   (prog1 (first (slot-value c 'items))     (setf (slot-value c 'items)           (rest (slot-value c 'items)))))  cl-user 7 > (setf circ1 (make-instance 'circular :items (list 1 2 3))) #<circular 40200017cb>  cl-user 8 > (next-item circ1) 1  cl-user 9 > (next-item circ1) 2  cl-user 10 > (next-item circ1) 3  cl-user 11 > (next-item circ1) 1  cl-user 12 > (next-item circ1) 2 

Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -