Ken Tilton <kentilton@gmail.com> wrote:
+---------------
| Slobodan Blazeski wrote:
| > I didn't read your whole post but your test cases seems fine.
| > (defun separate (lst)
| > (mapcar #'(lambda (n) (mapcar #'abs (remove-if-not n lst)))
| > (list #'plusp #'zerop #'minusp)))
|
| Traversing the list four times and a superfluous consing of the list?
| I'd give you a "B", but in a competitve setting with other students
| actually making an effort the curve would push this grade school hack
| down a grade and the OP gets a "C". Next thing you know the kid flunks
| out and comp.lang.lisp gets blamed and we have twenty years of HW Winter
| for Lisp.
+---------------
Yeah, Slobodan's version was so cute [albeit, as you note, horribly
inefficient] that I had to do a double-take before I could parse it.
So as long as we're offering homework solutions that won't be accepted
anyway [due to not using politically-correct recursive methods or
due to using "things we haven't covered yet"], why not have one
that's at least efficient and perspicuous *and* also incidentally
preserves the order of the input values? ;-}
(defun separate (list)
(loop for item in list
when (zerop item)
collect item into zeroes
else when (plusp item)
collect item into positives
else
collect (abs item) into negatives
finally (return (list positives zeroes negatives))))
-Rob
-----
Rob Warnock <rpw3@rpw3.org>
627 26th Avenue <URL:http://rpw3.org/>
San Mateo, CA 94403 (650)572-2607