Intersect bug

Q&A's, tips, howto's

Intersect bug

Postby tadeas » Tue Mar 04, 2014 8:38 am

Hi, if there's a dedicated bug tracker for newLISP please let me know and I'll repost it there.

There's a bug in intersect function:

Code: Select all
$ newlisp
newLISP v.10.5.4 64-bit on OSX IPv4/6 UTF-8, options: newlisp -h

> (set 'a '(2 2))
(2 2)
> (set 'b '(2 2 2))
(2 2 2)
> (intersect a b true)
(2 2)
> (intersect b a true)
(2 2 2)
tadeas
 
Posts: 3
Joined: Tue Mar 04, 2014 8:32 am

Re: Intersect bug

Postby cormullion » Tue Mar 04, 2014 10:07 am

Hi tadeas. Is that a bug?
cormullion
 
Posts: 2037
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W

Re: Intersect bug

Postby tadeas » Tue Mar 04, 2014 10:47 am

Now that I read intersect documentation again it looks like this behavior is correct.
In the second syntax, intersect returns a list of all elements in list-A that are also in list-B, without eliminating duplicates in list-A


But, then, intersect function is not doing an intersection (on the other hand nobody says it should). From what I remember from studies, intersection of {1 2 2 2 3} and {2 2} is unambiguously {2 2} .
tadeas
 
Posts: 3
Joined: Tue Mar 04, 2014 8:32 am

Re: Intersect bug

Postby cormullion » Tue Mar 04, 2014 11:02 am

Perhaps there are some variations in the definition of intersection...

With this definition: "All the elements of set A that are in set B":

Code: Select all
(intersect '(1 2 2 2 3) '(2 2) true)
;-> (2 2 2)


it looks correct. But "All the elements of set B that are in set A" should be returning '(2 2). Trouble is, A and B aren't sets... :)
cormullion
 
Posts: 2037
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W

Re: Intersect bug

Postby Lutz » Tue Mar 04, 2014 4:29 pm

When elements in the participating sets are unique, intersect works like the algebraic intersection and the position of sets in the expression is commutative. When not using the true parameter, lists are converted into unique collections sets.

Almost always intersect is used in newLISP to make a selection of elements in one set depending of elements in another set. The positions in the intersect expression are not commutative when elements in a set are not unique. This and the true parameter give us more possibilities selecting elements than using pure algebraic intersection on unique collections.
Lutz
 
Posts: 5279
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California

Re: Intersect bug

Postby tadeas » Tue Mar 04, 2014 5:30 pm

Very well, this is not a bug.

I made two mistakes:
1) I didn't properly read intersect documentation so I expected it to do something a bit different.
2) I didn't realize that the algebraic intersection doesn't really make sense on lists with the same element more times.

I'm sorry guys.
tadeas
 
Posts: 3
Joined: Tue Mar 04, 2014 8:32 am

Re: Intersect bug

Postby cormullion » Tue Mar 04, 2014 5:37 pm

No worries - it made me read that part of the manual again, so I gained something too... :)
cormullion
 
Posts: 2037
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W

Re: Intersect bug

Postby rickyboy » Tue Mar 04, 2014 6:21 pm

What cormullion said. :)

I'm not a beginner at newLISP, and I learned something. Thanks for your post, tadeas!
(λx. x x) (λx. x x)
rickyboy
 
Posts: 594
Joined: Fri Apr 08, 2005 7:13 pm
Location: Front Royal, Virginia


Return to newLISP in the real world

Who is online

Users browsing this forum: No registered users and 3 guests