Guest User


a guest
Dec 19th, 2019
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. ;; <14:33:15><jmercouris> hi guys, I have a problem with lists I can solve, but I am trying to think of the most efficient way to do this
  2. ;; <14:33:41><jmercouris> so, there is a list of elements "a" "b" "c" "d" "e"
  3. ;; <14:33:50> *** Bike (9bf735a7@ has joined channel #lisp
  4. ;; <14:34:06><jmercouris> I want to take some elements that meet some criteria X and move them to the front of a new list, then I want to append the rest of the elements that meet some criteria Y to the end of the list
  5. ;; <14:34:14><jmercouris> I could make a new list, however, that feels quite wasteful
  7. (defun append-cells (&rest cell-lists)
  8. (let ((head (first (first cell-lists)))
  9. (current (cons nil nil)))
  10. (dolist (cell-list cell-lists)
  11. (dolist (cell cell-list)
  12. (setf (cdr current) cell
  13. current (cdr current))))
  14. (setf (cdr current) nil)
  15. head))
  17. (defun reorder (list test-head test-tail)
  18. (loop
  19. :for cell :on list
  20. :if (funcall test-head (car cell))
  21. :collect cell :into head-cells
  22. :else if (funcall test-tail (car cell))
  23. :collect cell :into tail-cells
  24. :else
  25. :collect cell :into other-cells
  26. :finally (return (append-cells head-cells other-cells tail-cells))))
  28. (let ((list (list 0 'a "a" 1 "b" 'b 2 "c" :c 3.3)))
  29. (setf list (reorder list (function symbolp) (function integerp))))
  30. ;; --> (a b :c "a" "b" "c" 3.3 0 1 2)
Add Comment
Please, Sign In to add comment