Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;; <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
- ;; <14:33:41><jmercouris> so, there is a list of elements "a" "b" "c" "d" "e"
- ;; <14:33:50> *** Bike (9bf735a7@155.247.53.167) has joined channel #lisp
- ;; <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
- ;; <14:34:14><jmercouris> I could make a new list, however, that feels quite wasteful
- (defun append-cells (&rest cell-lists)
- (let ((head (first (first cell-lists)))
- (current (cons nil nil)))
- (dolist (cell-list cell-lists)
- (dolist (cell cell-list)
- (setf (cdr current) cell
- current (cdr current))))
- (setf (cdr current) nil)
- head))
- (defun reorder (list test-head test-tail)
- (loop
- :for cell :on list
- :if (funcall test-head (car cell))
- :collect cell :into head-cells
- :else if (funcall test-tail (car cell))
- :collect cell :into tail-cells
- :else
- :collect cell :into other-cells
- :finally (return (append-cells head-cells other-cells tail-cells))))
- (let ((list (list 0 'a "a" 1 "b" 'b 2 "c" :c 3.3)))
- (setf list (reorder list (function symbolp) (function integerp))))
- ;; --> (a b :c "a" "b" "c" 3.3 0 1 2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement