newLISP v.10.7.0 64-bit on Linux IPv4/6 UTF-8 libffi, options: newlisp -h
> (setf a '(4 5 3 6 7))
(4 5 3 6 7)
> (setf b '(3 1 2 0 5))
(3 1 2 0 5)
> (setf c '(4 0 1 2 5))
(4 0 1 2 5)
> (setf d '(0 1 2 0 5))
(0 1 2 0 5)
> (intersect a b c d)
(5 3)
I'll bet you were thinking about that like (or similar to):
(intersect (intersect a b) (intersect c d))
and not as
(intersect a b true)
which newlisp did :-)
Quote
intersect
syntax: (intersect list-A list-B)
syntax: (intersect list-A list-B bool)
If you want intersect more list:
> (set '@lst '((1 2 3 4) (2 3 4 5) (3 4 5 6) (4 5 6 7)))
((1 2 3 4) (2 3 4 5) (3 4 5 6) (4 5 6 7))
> (map (curry apply intersect) (explode @lst 2))
((2 3 4) (4 5 6))
> (apply intersect (map (curry apply intersect) (explode @lst 2)))
(4)
Quote from: "ssqq"If you want intersect more list:
> (set '@lst '((1 2 3 4) (2 3 4 5) (3 4 5 6) (4 5 6 7)))
((1 2 3 4) (2 3 4 5) (3 4 5 6) (4 5 6 7))
> (map (curry apply intersect) (explode @lst 2))
((2 3 4) (4 5 6))
> (apply intersect (map (curry apply intersect) (explode @lst 2)))
(4)
Or more simply:
> (set '@lst '((1 2 3 4) (2 3 4 5) (3 4 5 6) (4 5 6 7)))
((1 2 3 4) (2 3 4 5) (3 4 5 6) (4 5 6 7))
> (apply intersect @lst 2)
(4)
With kanen's example:
> (setf a '(4 5 3 6 7))
(4 5 3 6 7)
> (setf b '(3 1 2 0 5))
(3 1 2 0 5)
> (setf c '(4 0 1 2 5))
(4 0 1 2 5)
> (setf d '(0 1 2 0 5))
(0 1 2 0 5)
> (apply intersect (list a b c d) 2)
(5)
The intersect function only takes 2 lists, but you could use apply with a reduce 2 parameter to work on more lists:
> (setf a '(4 5 3 6 7))
(4 5 3 6 7)
> (setf b '(3 1 2 0 5))
(3 1 2 0 5)
> (setf c '(4 0 1 2 5))
(4 0 1 2 5)
> (setf d '(0 1 2 0 5))
(0 1 2 0 5)
>
> (apply intersect (list a b c d) 2)
(5)
> (setf a '(0 5 3 6 7))
(0 5 3 6 7)
> (apply intersect (list a b c d) 2)
(0 5)
> (setf a '(0 5 3 1 7))
(0 5 3 1 7)
> (apply intersect (list a b c d) 2)
(0 5 1)
Older LISPs have a function reduce to do the same. In newLISP that functionality is part of the apply function.
http://www.newlisp.org/downloads/newlisp_manual.html#intersect
http://www.newlisp.org/downloads/newlisp_manual.html#apply