From: mawhin
Subject: lisp is evil. In a good way. (Short circuiting intersection via the 	:test)
Date: 
Message-ID: <12f0280c-c751-42b1-8841-12a875971fa8@z27g2000prd.googlegroups.com>
Ok. I loved perl. But lisp? Oh mama!

I need to test that two sets of integers don't intersect. So I write
this.

(defun simply-connected-p (sw-and-p-1 sw-and-p-2)
      (not (intersection (through-set sw-and-p-1) (through-set sw-and-
p-2)
                         :test #'=)))

But I looked at it, and looked at it, and it computes the full
intersection every time. What I want it to do is return as soon as we
know that one integer is in both sets. So I did this:

(defun simply-connected-p (sw-and-p-1 sw-and-p-2)
      (not (intersection (through-set sw-and-p-1) (through-set sw-and-
p-2)
                         :test #'(lambda (a b)
                                   (and (= a b) (return-from simply-
connected-p NIL))))))

So if the intersection is empty, the function returns (not
(intersection set1 set2)), which is T. If the intersection's not
empty, we return very early with NIL. You shouldn't be able to do
this. It's not right. I think I could get to love lisp.

From: Rainer Joswig
Subject: Re: lisp is evil. In a good way. (Short circuiting intersection via the :test)
Date: 
Message-ID: <joswig-02FFB3.16240312122008@news-europe.giganews.com>
In article 
<····································@z27g2000prd.googlegroups.com>,
 mawhin <···············@gmail.com> wrote:

> Ok. I loved perl. But lisp? Oh mama!
> 
> I need to test that two sets of integers don't intersect. So I write
> this.
> 
> (defun simply-connected-p (sw-and-p-1 sw-and-p-2)
>       (not (intersection (through-set sw-and-p-1) (through-set sw-and-
> p-2)
>                          :test #'=)))
> 
> But I looked at it, and looked at it, and it computes the full
> intersection every time. What I want it to do is return as soon as we
> know that one integer is in both sets. So I did this:
> 
> (defun simply-connected-p (sw-and-p-1 sw-and-p-2)
>       (not (intersection (through-set sw-and-p-1) (through-set sw-and-
> p-2)
>                          :test #'(lambda (a b)
>                                    (and (= a b) (return-from simply-
> connected-p NIL))))))
> 
> So if the intersection is empty, the function returns (not
> (intersection set1 set2)), which is T. If the intersection's not
> empty, we return very early with NIL. You shouldn't be able to do
> this. It's not right. I think I could get to love lisp.


Style: INTERSECTION returns a list. NULL tests for an empty list.

Another variant:

CL-USER 79 > (defun no-intersection-p (list1 list2) 
                 (notany (lambda (item) (find item list1)) list2))
NO-INTERSECTION-P

CL-USER 80 > (no-intersection-p '(1 2 3) '(4 5 6))
T

CL-USER 81 > (no-intersection-p '(1 2 3) '(4 5 6 1))
NIL

CL-USER 82 > (no-intersection-p '(1 2 3 4) '(4 5 6))
NIL

-- 
http://lispm.dyndns.org/
From: mawhin
Subject: Re: lisp is evil. In a good way. (Short circuiting intersection via 	the :test)
Date: 
Message-ID: <f907b930-af17-4553-8eef-dfe7a8dbf1a2@q30g2000prq.googlegroups.com>
On Dec 12, 3:24 pm, Rainer Joswig <······@lisp.de> wrote:
> In article
> <····································@z27g2000prd.googlegroups.com>,
>
>
>
>  mawhin <···············@gmail.com> wrote:
> > Ok. I loved perl. But lisp? Oh mama!
>
> > I need to test that two sets of integers don't intersect. So I write
> > this.
>
> > (defun simply-connected-p (sw-and-p-1 sw-and-p-2)
> >       (not (intersection (through-set sw-and-p-1) (through-set sw-and-
> > p-2)
> >                          :test #'=)))
>
> > But I looked at it, and looked at it, and it computes the full
> > intersection every time. What I want it to do is return as soon as we
> > know that one integer is in both sets. So I did this:
>
> > (defun simply-connected-p (sw-and-p-1 sw-and-p-2)
> >       (not (intersection (through-set sw-and-p-1) (through-set sw-and-
> > p-2)
> >                          :test #'(lambda (a b)
> >                                    (and (= a b) (return-from simply-
> > connected-p NIL))))))
>
> > So if the intersection is empty, the function returns (not
> > (intersection set1 set2)), which is T. If the intersection's not
> > empty, we return very early with NIL. You shouldn't be able to do
> > this. It's not right. I think I could get to love lisp.
>
> Style: INTERSECTION returns a list. NULL tests for an empty list.
>
> Another variant:
>
> CL-USER 79 > (defun no-intersection-p (list1 list2)
>                  (notany (lambda (item) (find item list1)) list2))
> NO-INTERSECTION-P
>
> CL-USER 80 > (no-intersection-p '(1 2 3) '(4 5 6))
> T
>
> CL-USER 81 > (no-intersection-p '(1 2 3) '(4 5 6 1))
> NIL
>
> CL-USER 82 > (no-intersection-p '(1 2 3 4) '(4 5 6))
> NIL
>
> --http://lispm.dyndns.org/

And an order of magnitude faster, to boot. Your version wins. And it's
better.

:-))

Mart
From: Rainer Joswig
Subject: Re: lisp is evil. In a good way. (Short circuiting intersection via the :test)
Date: 
Message-ID: <joswig-26D210.17502912122008@news-europe.giganews.com>
In article 
<····································@q30g2000prq.googlegroups.com>,
 mawhin <···············@gmail.com> wrote:

> On Dec 12, 3:24 pm, Rainer Joswig <······@lisp.de> wrote:
> > In article
> > <····································@z27g2000prd.googlegroups.com>,
> >
> >
> >
> >  mawhin <···············@gmail.com> wrote:
> > > Ok. I loved perl. But lisp? Oh mama!
> >
> > > I need to test that two sets of integers don't intersect. So I write
> > > this.
> >
> > > (defun simply-connected-p (sw-and-p-1 sw-and-p-2)
> > >       (not (intersection (through-set sw-and-p-1) (through-set sw-and-
> > > p-2)
> > >                          :test #'=)))
> >
> > > But I looked at it, and looked at it, and it computes the full
> > > intersection every time. What I want it to do is return as soon as we
> > > know that one integer is in both sets. So I did this:
> >
> > > (defun simply-connected-p (sw-and-p-1 sw-and-p-2)
> > >       (not (intersection (through-set sw-and-p-1) (through-set sw-and-
> > > p-2)
> > >                          :test #'(lambda (a b)
> > >                                    (and (= a b) (return-from simply-
> > > connected-p NIL))))))
> >
> > > So if the intersection is empty, the function returns (not
> > > (intersection set1 set2)), which is T. If the intersection's not
> > > empty, we return very early with NIL. You shouldn't be able to do
> > > this. It's not right. I think I could get to love lisp.
> >
> > Style: INTERSECTION returns a list. NULL tests for an empty list.
> >
> > Another variant:
> >
> > CL-USER 79 > (defun no-intersection-p (list1 list2)
> >                  (notany (lambda (item) (find item list1)) list2))
> > NO-INTERSECTION-P
> >
> > CL-USER 80 > (no-intersection-p '(1 2 3) '(4 5 6))
> > T
> >
> > CL-USER 81 > (no-intersection-p '(1 2 3) '(4 5 6 1))
> > NIL
> >
> > CL-USER 82 > (no-intersection-p '(1 2 3 4) '(4 5 6))
> > NIL
> >
> > --http://lispm.dyndns.org/
> 
> And an order of magnitude faster, to boot. Your version wins. And it's
> better.
> 
> :-))
> 
> Mart

Thanks for the feedback. :-)

-- 
http://lispm.dyndns.org/
From: ·········@yahoo.com
Subject: Re: lisp is evil. In a good way. (Short circuiting intersection via 	the :test)
Date: 
Message-ID: <ad811375-acba-489d-aaab-5dc5012ffd06@a12g2000yqm.googlegroups.com>
On Dec 12, 9:24 am, Rainer Joswig <······@lisp.de> wrote:
> In article
> <····································@z27g2000prd.googlegroups.com>,
>
>
>
>  mawhin <···············@gmail.com> wrote:
> > Ok. I loved perl. But lisp? Oh mama!
>
> > I need to test that two sets of integers don't intersect. So I write
> > this.
>
> > (defun simply-connected-p (sw-and-p-1 sw-and-p-2)
> >       (not (intersection (through-set sw-and-p-1) (through-set sw-and-
> > p-2)
> >                          :test #'=)))
>
> > But I looked at it, and looked at it, and it computes the full
> > intersection every time. What I want it to do is return as soon as we
> > know that one integer is in both sets. So I did this:
>
> > (defun simply-connected-p (sw-and-p-1 sw-and-p-2)
> >       (not (intersection (through-set sw-and-p-1) (through-set sw-and-
> > p-2)
> >                          :test #'(lambda (a b)
> >                                    (and (= a b) (return-from simply-
> > connected-p NIL))))))
>
> > So if the intersection is empty, the function returns (not
> > (intersection set1 set2)), which is T. If the intersection's not
> > empty, we return very early with NIL. You shouldn't be able to do
> > this. It's not right. I think I could get to love lisp.
>
> Style: INTERSECTION returns a list. NULL tests for an empty list.
>
> Another variant:
>
> CL-USER 79 > (defun no-intersection-p (list1 list2)
>                  (notany (lambda (item) (find item list1)) list2))
> NO-INTERSECTION-P
>
> CL-USER 80 > (no-intersection-p '(1 2 3) '(4 5 6
>
> CL-USER 81 > (no-intersection-p '(1 2 3) '(4 5 6 1))
> NIL
>
> CL-USER 82 > (no-intersection-p '(1 2 3 4) '(4 5 6))
> NIL
>
> --http://lispm.dyndns.org/

Ruby:

irb(main):001:0> [6,3,2,1] & [0,2,4,6]
=> [6, 2]
From: Alex Mizrahi
Subject: Re: lisp is evil. In a good way. (Short circuiting intersection via the :test)
Date: 
Message-ID: <4943cd7d$0$90269$14726298@news.sunsite.dk>
 wx> Ruby:

 wx> irb(main):001:0> [6,3,2,1] & [0,2,4,6]
 wx> => [6, 2]

Lisp:

 (intersection '(0 1 2) '(1 2 3))
   => (1 2)

no need for weird symbols. 
From: Alex Mizrahi
Subject: Re: lisp is evil. In a good way. (Short circuiting intersection via the :test)
Date: 
Message-ID: <4942cc6f$0$90274$14726298@news.sunsite.dk>
 m> But I looked at it, and looked at it, and it computes the full
 m> intersection every time.

doesn['t function name INTERSECTION imply this? if it was called
intersects-p, then you could expect different behaviour..