Last active
June 2, 2024 05:42
-
-
Save amno1/d6f9af2f2d7e6352c971903f70594214 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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