For example, if we have a function such as cut-list-if, which can cut
a list between elements according to a predicate taking two successive elements in the list, such as:
(cut-list-if (lambda (a b) (or a b))
'(nil nil nil a nil nil nil a b nil nil nil
a b c nil nil nil a b c d e nil))
((NIL NIL NIL) (A) (NIL NIL NIL) (A) (B) (NIL NIL NIL) (A) (B) (C)(NIL NIL NIL) (A) (B) (C) (D) (E) (NIL))
Oops, I forgot to present a CUT-LIST-IF:
(defun cut-list-if (predicate list)
(loop
:with results = '()
:with sublist = '()
:for (a b) :on list
:do (if (funcall predicate a b)
(progn
(push a sublist)
(push sublist results)
(setf sublist '()))
(push a sublist))
:finally (when sublist (push sublist results)) (return (reverse results))))
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 459 |
Nodes: | 16 (2 / 14) |
Uptime: | 144:05:43 |
Calls: | 9,339 |
Calls today: | 10 |
Files: | 13,538 |
Messages: | 6,083,393 |
Posted today: | 1 |