intersect bug?

Q&A's, tips, howto's

intersect bug?

Postby kanen » Thu Aug 04, 2016 9:49 pm

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 .
kanen
 
Posts: 145
Joined: Thu Mar 25, 2010 6:24 pm

Re: intersect bug?

Postby ralph.ronnquist » Thu Aug 04, 2016 11:30 pm

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 :-)
ralph.ronnquist
 
Posts: 209
Joined: Mon Jun 02, 2014 1:40 am
Location: Melbourne, Australia

Re: intersect bug?

Postby ssqq » Fri Aug 05, 2016 8:04 am

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)

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

Re: intersect bug?

Postby rickyboy » Fri Aug 05, 2016 12:08 pm

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)
rickyboy
 
Posts: 595
Joined: Fri Apr 08, 2005 7:13 pm
Location: Front Royal, Virginia

Re: intersect bug?

Postby Lutz » Fri Aug 05, 2016 1:13 pm

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
Lutz
 
Posts: 5279
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California


Return to newLISP in the real world

Who is online

Users browsing this forum: No registered users and 1 guest

cron