Skip to content

Instantly share code, notes, and snippets.

@amno1
Last active June 2, 2024 05:42
Show Gist options
  • Save amno1/d6f9af2f2d7e6352c971903f70594214 to your computer and use it in GitHub Desktop.
Save amno1/d6f9af2f2d7e6352c971903f70594214 to your computer and use it in GitHub Desktop.
;; no consing one iteration
(defun rotatel (list)
(let ((new (cdr list)))
(setf (cdr list) nil)
(setf new (nconc new list))))
;; no consing two iterations
(defun rotatel (list)
(let ((first (pop list))
(rlist (nreverse list)))
(push first rlist)
(nreverse rlist)))
;; one cons one iteration
(defmacro while (test &rest body)
`(do () ((not ,test) (values)) ,@body))
(defun rotatel (list)
(let ((first (pop list))
(new list))
(while (cdr list) (setf list (cdr list)))
(setf (cdr list) (cons first nil))
new))
;; no consing, no iteration, circular
(defun rotatel (list)
(setf list (cdr list)))
(defun make-circle (list)
(setf (cdr (last list)) list))
;; test
(setf *print-circle* t)
(setq mylist '((a . 1) (b . 2) (c . 3)))
(make-circle mylist)
(format t "~a~%" mylist)
(setf mylist (rotatel mylist))
(setf mylist (rotatel mylist))
(setf mylist (rotatel mylist))
(setf mylist (rotatel mylist))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment