From: Fred Gilham
Subject: every-other revisited (with series)
Date: 
Message-ID: <u7677a3keg.fsf_-_@snapdragon.csl.sri.com>
For those who are interested, here are two versions of the
`every-other' function we were discussing a couple months ago, done
for amusement value with the SERIES package that was mentioned
recently:


(defun every-other (list &key (start 0))
  (collect
   (choose (series t nil)
	   (scan (nthcdr start list)))))


This one uses SERIES to create a series of (t nil t nil ...); CHOOSE
uses this series to determine whether or not to take the current item
from the series created from the original list by SCAN.  This was the
first way I figured out how to do it.


(defun every-other-s2 (list &key (start 0))
  (collect
   (#Mcar
    (scan-fn t #'(lambda () (nthcdr start list)) #'cddr #'null))))


This way uses SCAN-FN to create a series of lists; each list is
created from the previous using CDDR.  `#Mcar' is used to create a
series from that series consisting of the first element of each list.
I came up with this method once I figured out how SCAN-FN and #M work.

I like the way the first one looks better than the second.  It seems
to express one view of the idea of `every-other'-ness rather nicely.
I'd be interested in seeing how SERIES experts (if there are any) do
it.

Timings seem to be a little better than the recursive version and a
little worse than the loop macro version:

Recursive version:


(defun every-other_3 (list-in &key (start 0))
  (if (not (listp list-in))
      nil
    (let ((work-list (nthcdr start list-in)))
      (every-other-aux work-list nil))))

(defun every-other-aux (list-in list-out)
  (if (null list-in)
      (nreverse list-out)
    (progn
      (push (car list-in) list-out)
      (every-other-aux (cddr list-in) list-out))))


Loop macro version:


(defun every-other_4 (list &key (start 0))
  (loop for item in (nthcdr start list) by #'cddr
      collect item))


-- 
Fred Gilham                                     ······@csl.sri.com
How many Analytic Philosophers does it take to change a light bulb? 
None---it's a pseudo-problem. Light bulbs give off light (hence the
name). If the bulb was broken and wasn't giving off light, it wouldn't
be a 'light bulb' now would it? (Oh, where has rigor gone?!)