intersect bug?

Q&A's, tips, howto's
Post Reply
kanen
Posts: 145
Joined: Thu Mar 25, 2010 6:24 pm
Contact:

intersect bug?

Post 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)
. Kanen Flowers http://kanen.me .

ralph.ronnquist
Posts: 216
Joined: Mon Jun 02, 2014 1:40 am
Location: Melbourne, Australia

Re: intersect bug?

Post 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 :-)

ssqq
Posts: 88
Joined: Sun May 04, 2014 12:49 pm

Re: intersect bug?

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


rickyboy
Posts: 595
Joined: Fri Apr 08, 2005 7:13 pm
Location: Front Royal, Virginia

Re: intersect bug?

Post 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)
(λx. x x) (λx. x x)

Lutz
Posts: 5279
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Re: intersect bug?

Post 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

Post Reply