Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (defmacro pop-n (place n &environment env)
- (multiple-value-bind (vars vals stores setter getter)
- (get-setf-expansion place env)
- (let ((old-head (gensym "OLDHEAD-"))
- (new-head (gensym "NEWHEAD-"))
- (num (gensym "N-")))
- `(let ,(mapcar #'list vars vals)
- (let* ((,old-head ,getter)
- (,num ,n)
- (,new-head (nthcdr ,num ,old-head))
- (,(first stores) ,new-head)
- ,@(rest stores))
- ,setter
- (when ,new-head
- (setf (cdr (nthcdr (1- ,num) ,old-head)) nil))
- ,old-head
- )))))
- ;; (defun test1 (list)
- ;; (loop while list
- ;; for sublist = (pop-n list 4)
- ;; collect sublist))
- ;; (let ((inputs
- ;; (list (list 1 2 3 4)
- ;; (list 1 2 3 4 5 6 7 8)
- ;; (list 1 2 3 4 5 6 7)
- ;; (list 1)
- ;; (list))))
- ;; (dolist (input inputs)
- ;; (print (test1 input))))
- ;; (defstruct container
- ;; (list nil :type list))
- ;; (defun test2 (c)
- ;; (loop while (container-list c)
- ;; for sublist = (pop-n (container-list c) 4)
- ;; collect sublist))
- ;; (let ((inputs
- ;; (list (list 1 2 3 4)
- ;; (list 1 2 3 4 5 6 7 8)
- ;; (list 1 2 3 4 5 6 7)
- ;; (list 1)
- ;; (list))))
- ;; (dolist (input inputs)
- ;; (print (test2 (make-container :list input)))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement