(loop for x in '(3 5 7)
for y in '(2 5 8)
when (= x y)
collect (cons x y))
(loop for x in '(3 5 7)
for y in '(2 5 8)
when (= x y)
collect (cons x y))
What if there were more than two lists?
This will handle any number of lists:
Scheme:
(filter-map
(lambda xs (and (apply = xs) xs))
'(0 3 5 7 9)
'(0 2 5.0 8 9.0)
'(2 4 5 8 9))
((5 5.0 5) (9 9.0 9))
(mappend [iff = [chain list list]]'(0 3 5 7 9)
At least that's the way it was in R5RS. I don't have a more recent
version of the specification handy.
Anyway, I whipped one up in a few lines that not only pulls n items
off the list, but it returns the remaining items as well. Let me know >>what you think. I was kind of surprised that I was able to do it
without the use of an accumulator parameter.
(defun first-n (n list)
"Splits the list after the first n elements"
(if (= 0 n)
(values '() list)
(multiple-value-bind (from-front rest) (first-n (1- n) (rest
list))
(values (cons (first list) from-front) rest))))
I would probably do something like this instead:
(defun first-n (n list)
(loop for i below n for (a . d) on list
collect a into x
finally (return (values x d))))
Niiiiiiice.
| Sysop: | Amessyroom |
|---|---|
| Location: | Fayetteville, NC |
| Users: | 70 |
| Nodes: | 6 (0 / 6) |
| Uptime: | 37:48:40 |
| Calls: | 948 |
| Calls today: | 2 |
| Files: | 1,325 |
| Messages: | 280,560 |