Skip to content

Instantly share code, notes, and snippets.

@phoe
Created July 28, 2017 12:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phoe/0ae2bdf01366431512326a2ee49b36c1 to your computer and use it in GitHub Desktop.
Save phoe/0ae2bdf01366431512326a2ee49b36c1 to your computer and use it in GitHub Desktop.
;; thanks, beach!
(defun split-list-if (predicate list)
"Destructively splits the provided list into two lists: one contains the
elements of the original list that satisfy the predicate, the other contains
elements that do not satisfy the predicate. The order of the original elements
is preserved."
(loop with result1 = '()
with result2 = '()
with rest = list
until (null rest)
do (let ((temp rest))
(pop rest)
(if (funcall predicate (car temp))
(setf (cdr temp) result1
result1 temp)
(setf (cdr temp) result2
result2 temp)))
finally (return (values (nreverse result1)
(nreverse result2)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment