Skip to content

Instantly share code, notes, and snippets.

@lispm
Created September 4, 2016 05:41
Show Gist options
  • Save lispm/abe13010c38caa7946e3a8cd1540e385 to your computer and use it in GitHub Desktop.
Save lispm/abe13010c38caa7946e3a8cd1540e385 to your computer and use it in GitHub Desktop.
(defun circular-mapc (function list)
(loop for fast = list then (cddr fast)
for slow = list then (cdr slow)
do
(funcall function (car slow))
(when (eq (cddr fast) (cdr slow))
(return-from circular-mapc nil))))
(defun circular (items)
(check-type items cons)
(setf (cdr (last items)) items)
items)
(defun circular-member-p (item list)
(circular-mapc (lambda (e)
(when (eql item e)
(return-from circular-member t)))
list))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment