[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.lisp

intersection in nested lists

snrh4x0r

10/15/2015 7:49:00 PM

I've been trying to write a lisp code that prints intersecting elements. But there is a exception. It can be nested list. Below you can see expected and gotten output.

Gotten output:

> (intersect '(a b c) '( b a c))
a b c
>(intersect '(a (b c) d) '(a b c d))
a d
>(intersect '(a b (c f)) '(a d (c f) e) )
a

Expected output:

> (intersect '(a b c) '( b a c))
a b c
>(intersect '(a (b c) d) '(a b c d))
a d
>(intersect '(a b (c f)) '(a d (c f) e) )
a (cf)

As you see, if there are two same lists in the lists it should be printed. If there are same elements in two lists but one and more often in other list in list, it shouldn't be printed as you see in the second. How can I get the expected output? Can be the code fixed?

(defun intersect (A B)
(if (eq A ())
A
(if (member (first A) B)
(cons (first A) (intersect (rest A) B))
(intersect (rest A) B))))


I have to write my intersection function which is intersect. How can the function fixed as expected. I know the default function can do that but what about my function?
11 Answers

William James

10/15/2015 9:27:00 PM

0

snrh4x0r@gmail.com wrote:

> Gotten output:
>
> > (intersect '(a b c) '( b a c))
> a b c
> >(intersect '(a (b c) d) '(a b c d))
> a d
> >(intersect '(a b (c f)) '(a d (c f) e) )
> a
>
> Expected output:
>
> > (intersect '(a b c) '( b a c))
> a b c
> >(intersect '(a (b c) d) '(a b c d))
> a d
> >(intersect '(a b (c f)) '(a d (c f) e) )
> a (cf)
>
> As you see, if there are two same lists in the lists it should be printed.
> If there are same elements in two lists but one and more often in other
> list in list, it shouldn't be printed as you see in the
> second. How can I get the expected output? Can be the code fixed?
>
> (defun intersect (A B)
> (if (eq A ())
> A
> (if (member (first A) B)
> (cons (first A) (intersect (rest A) B))
> (intersect (rest A) B))))


Hint:

CL-USER(20): (eql '(c d) '(c d))
NIL
CL-USER(21): (equal '(c d) '(c d))
T



Gauche Scheme:

(use srfi-1 :only (lset-intersection))
(lset-intersection equal? '(a b (c f)) '(a d (c f) e))
===>
(a (c f))

NewLisp:

> (intersect '(a b (c d)) '(a (c d) e f))
(a (c d))

Portable Standard Lisp:

1 lisp> (intersection '(a b (c d)) '(a (c d) e f))
(a (c d))

--
The Authoritarian Personality extends beyond the attempt to pathologize
cohesive gentile groups to pathologizing adaptive gentile behavior in general.
The principal intellectual difficulty is that behavior that is critical to
Judaism as a successful group evolutionary strategy is conceptualized as
pathological in gentiles. --- Dr. Kevin MacDonald; "The Frankfurt School of
Social Research and the Pathologization of Gentile Group Allegiances"

Jocelyn Fréchot

10/15/2015 9:50:00 PM

0

On 15/10/2015 21:49, snrh4x0r@gmail.com wrote:

> Expected output:
>
> > (intersect '(a b c) '( b a c))
> a b c
> >(intersect '(a (b c) d) '(a b c d))
> a d
> >(intersect '(a b (c f)) '(a d (c f) e) )
> a (cf)
>
> As you see, if there are two same lists in the lists it should be printed. If there are same elements in two lists but one and more often in other list in list, it shouldn't be printed as you see in the second. How can I get the expected output? Can be the code fixed?
>
> (defun intersect (A B)
> (if (eq A ())
> A
> (if (member (first A) B)
> (cons (first A) (intersect (rest A) B))
> (intersect (rest A) B))))

Just a quick search-and-fix:

CL-USER> (defun intersect (a b)
(cond ((eq a ())
a)
((member (first a) b)
(cons (first a) (intersect (rest a) b)))
(t
(intersect (rest a) b))))
INTERSECT

CL-USER> (trace intersect)
(INTERSECT)

CL-USER> (intersect '(a b (c f)) '(a d (c f) e))
0: (INTERSECT (A B (C F)) (A D (C F) E))
1: (INTERSECT (B (C F)) (A D (C F) E))
2: (INTERSECT ((C F)) (A D (C F) E))
3: (INTERSECT NIL (A D (C F) E))
3: INTERSECT returned NIL
2: INTERSECT returned NIL
1: INTERSECT returned NIL
0: INTERSECT returned (A)
(A)

CL-USER> (defun intersect (a b)
(cond ((eq a ())
a)
;; Donâ??t expect (EQL '(C F) '(C F)) to be true.
((member (first a) b :test #'equal)
(cons (first a) (intersect (rest a) b)))
(t
(intersect (rest a) b))))
WARNING: redefining COMMON-LISP-USER::INTERSECT in DEFUN
INTERSECT

CL-USER> (intersect '(a b (c f)) '(a d (c f) e))
0: (INTERSECT (A B (C F)) (A D (C F) E))
1: (INTERSECT (B (C F)) (A D (C F) E))
2: (INTERSECT ((C F)) (A D (C F) E))
3: (INTERSECT NIL (A D (C F) E))
3: INTERSECT returned NIL
2: INTERSECT returned ((C F))
1: INTERSECT returned ((C F))
0: INTERSECT returned (A (C F))
(A (C F))

--
Jocelyn Fréchot

William James

10/15/2015 11:07:00 PM

0

Jocelyn Fréchot wrote:

> CL-USER> (defun intersect (a b)
> (cond ((eq a ())
> a)
> ;; Donâ??t expect (EQL '(C F) '(C F)) to be true.
> ((member (first a) b :test #'equal)
> (cons (first a) (intersect (rest a) b)))
> (t
> (intersect (rest a) b))))

Why did you do his homework?

Barry Margolin

10/16/2015 12:32:00 AM

0

In article <8c917221-d59c-4835-959f-c804e4217eae@googlegroups.com>,
snrh4x0r@gmail.com wrote:

> I've been trying to write a lisp code that prints intersecting elements. But
> there is a exception. It can be nested list. Below you can see expected and
> gotten output.

I already answered this when you posted the same code on StackOverflow.

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***

taruss

10/16/2015 4:51:00 PM

0

On Thursday, October 15, 2015 at 12:49:24 PM UTC-7, snrh...@gmail.com wrote:
> I've been trying to write a lisp code that prints intersecting elements. But there is a exception. It can be nested list. Below you can see expected and gotten output.
>
[snip]
> (defun intersect (A B)
> (if (eq A ())
> A
> (if (member (first A) B)
> (cons (first A) (intersect (rest A) B))
> (intersect (rest A) B))))
>
>
> I have to write my intersection function which is intersect. How can the function fixed as expected. I know the default function can do that but what about my function?

You should read https://www.cs.cmu.edu/Groups/AI/html/cltl/clm/n... about equality predicates in lisp.

Steve G

10/23/2015 4:54:00 AM

0

snrh4x0r@gmail.com writes:

< I've been trying to write a lisp code that prints intersecting elements. But
< there is a exception. It can be nested list. Below you can see expected and
< gotten output.

It's been a long time since I've had a hand at lisp - anywayz.
Try to flatten the list first. The following is written in emacs-lisp.

;;;; Probably from Norvig AIP
(defun flatten (l &optional result)
(cond ((consp l)
(flatten (car l)
(flatten (cdr l) result)))
((null l) result)
(t (if (null l)
result
(cons l result)))))

;; (flatten *aa* ())
;; => (a b c f)

;; (flatten '(a b (c f (1 2) e) r))
;; => (a b c f 1 2 e r)


P.S. Anyone have an extra mouse for Symbolics XL12001?

Matthew Carter

10/23/2015 5:41:00 AM

0

Steve G <loft@centurylink.net> writes:

> snrh4x0r@gmail.com writes:
>
> < I've been trying to write a lisp code that prints intersecting
> elements. But < there is a exception. It can be nested list. Below you
> can see expected and < gotten output.
>
> It's been a long time since I've had a hand at lisp - anywayz.
> Try to flatten the list first. The following is written in emacs-lisp.
>
> ;;;; Probably from Norvig AIP
> (defun flatten (l &optional result)
> (cond ((consp l)
> (flatten (car l)
> (flatten (cdr l) result)))
> ((null l) result)
> (t (if (null l)
> result
> (cons l result)))))
>
> ;; (flatten *aa* ())
> ;; => (a b c f)
>
> ;; (flatten '(a b (c f (1 2) e) r))
> ;; => (a b c f 1 2 e r)
>
>
> P.S. Anyone have an extra mouse for Symbolics XL12001?
>

Decided to have some fun with a flatten:

(defun flatten-lol (list)
(read-from-string (format nil "(~a)"
(remove-if #'(lambda (l) (member l '(#\( #\))))
(princ-to-string list)))))


The next is using my glyphs package (available in Quicklisp!):

(Æ? flatty α â?? (read-from-string (format nil "(~a)"
(funcall (λ ~"[\(\)]"~ â?? |""|) (princ-to-string α)))))

--
Matthew Carter (m@ahungry.com)
http://a...

William James

10/23/2015 6:09:00 AM

0

Steve G. wrote:

> Try to flatten the list first. The following is written in emacs-lisp.
>
> ;;;; Probably from Norvig AIP
> (defun flatten (l &optional result)
> (cond ((consp l)
> (flatten (car l)
> (flatten (cdr l) result)))
> ((null l) result)
> (t (if (null l)
> result
> (cons l result)))))
>
> ;; (flatten aa ())
> ;; => (a b c f)
>
> ;; (flatten '(a b (c f (1 2) e) r))
> ;; => (a b c f 1 2 e r)

Gauche Scheme:

(define (flatten x)
(if (pair? x)
(append-map flatten x)
(list x)))

gosh> (flatten '(((a (b)) (c) d)))
(a b c d)

--
For two years ... he was held in solitary confinement in the Toronto West
Detention Centre, on the pretext that he is a threat to national security....
[A] court in Mannheim sentenced him to five years imprisonment for the crime of
"popular incitement" under Germany's notorious "Holocaust denial" statute.
http://www.revisionists.com/revisionists/z...

William James

10/23/2015 5:33:00 PM

0

WJ wrote:

> Steve G. wrote:
>
> > Try to flatten the list first. The following is written in emacs-lisp.
> >
> > ;;;; Probably from Norvig AIP
> > (defun flatten (l &optional result)
> > (cond ((consp l)
> > (flatten (car l)
> > (flatten (cdr l) result)))
> > ((null l) result)
> > (t (if (null l)
> > result
> > (cons l result)))))
> >
> > ;; (flatten aa ())
> > ;; => (a b c f)
> >
> > ;; (flatten '(a b (c f (1 2) e) r))
> > ;; => (a b c f 1 2 e r)
>
> Gauche Scheme:
>
> (define (flatten x)
> (if (pair? x)
> (append-map flatten x)
> (list x)))
>
> gosh> (flatten '(((a (b)) (c) d)))
> (a b c d)

Not quite right.

gosh> (flatten '())
(())

(define (flatten x)
(cond ((pair? x) (append-map flatten x))
((null? x) x)
(else (list x))))

gosh> (flatten '())
()
gosh> (flatten '(a () b c))
(a b c)
gosh> (flatten '(((a (b)) (c) d) ()) )
(a b c d)

--
Africans gang-rape and clitorectomize Finnish girl; government arrests Finn
whom they accuse of complaining:
conservative-headlines.com/2009/03/another-european-awaits-extradition-for-hate-speech

taruss

10/26/2015 5:43:00 PM

0

On Thursday, October 22, 2015 at 10:41:09 PM UTC-7, Matthew Carter wrote:
>
> Decided to have some fun with a flatten:
>
> (defun flatten-lol (list)
> (read-from-string (format nil "(~a)"
> (remove-if #'(lambda (l) (member l '(#\( #\))))
> (princ-to-string list)))))
>
>
> The next is using my glyphs package (available in Quicklisp!):
>
> (ƒ flatty a ? (read-from-string (format nil "(~a)"
> (funcall (? ~"[\(\)]"~ ? |""|) (princ-to-string a)))))

(flatten-lol '(a b |psst(a secret!)| and some #\( characters #\)))

:-p