Page 1 of 1

intersect bug?

Posted: Thu Aug 04, 2016 9:49 pm
by kanen

Code: Select all

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)

Re: intersect bug?

Posted: Thu Aug 04, 2016 11:30 pm
by ralph.ronnquist
I'll bet you were thinking about that like (or similar to):

Code: Select all

(intersect (intersect a b) (intersect c d))
and not as

Code: Select all

(intersect a b true)
which newlisp did :-)

Re: intersect bug?

Posted: Fri Aug 05, 2016 8:04 am
by ssqq
intersect
syntax: (intersect list-A list-B)
syntax: (intersect list-A list-B bool)
If you want intersect more list:

Code: Select all

> (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)


Re: intersect bug?

Posted: Fri Aug 05, 2016 12:08 pm
by rickyboy
ssqq wrote:If you want intersect more list:

Code: Select all

> (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:

Code: Select all

> (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:

Code: Select all

> (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)

Re: intersect bug?

Posted: Fri Aug 05, 2016 1:13 pm
by Lutz
The intersect function only takes 2 lists, but you could use apply with a reduce 2 parameter to work on more lists:

Code: Select all

> (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/newlis ... #intersect
http://www.newlisp.org/downloads/newlis ... html#apply