3

I have 2 lists of elements '(a b c) '(d b f) and want to find differences, union, and intersection in one result. Is that possible? How?

I wrote a member function that checks if there is a car of the first list in the second list, but I can't throw a member to the new list.

(define (checkResult lis1 lis2)
  (cond...........

))
(checkresult '( a b c) '(d b f))

My result should be (( a c) (d f) (a b c d f) (b)).

Eli Barzilay
  • 29,301
  • 3
  • 67
  • 110

4 Answers4

10

Like others have said, all you need to do is create separate functions to compute the intersection, union, and subtraction of the two sets, and call them from checkresult:

(define (checkresult a b)
  (list (subtract a b)
        (subtract b a)
        (union a b)
        (intersect a b)))

Here are some example union, intersection, and subtraction functions:

(define (element? x lst)
  (cond ((null? lst) #f)
        ((eq? x (car lst)) #t)
        (#t (element? x (cdr lst)))))

(define (union a b)
  (cond ((null? b) a)
        ((element? (car b) a)
         (union a (cdr b)))
        (#t (union (cons (car b) a) (cdr b)))))

(define (intersect a b)
  (if (null? a) '()
      (let ((included (element? (car a) b)))
        (if (null? (cdr a))
            (if included a '())
            (if included
                (cons (car a) (intersect (cdr a) b))
                (intersect (cdr a) b))))))

(define (subtract a b)
  (cond ((null? a) '())
        ((element? (car a) b)
         (subtract (cdr a) b))
        (#t (cons (car a) (subtract (cdr a) b)))))

Note: since these are sets and order doesn't matter, the results are not sorted. Also, the functions assume that the inputs are sets, and therefore don't do any duplicate checking beyond what's required for union.

Kyle Cronin
  • 77,653
  • 43
  • 148
  • 164
  • 6
    And it was fun to do! I'll answer any questions I can, including homework. It's not my job to make sure students don't cheat. – Kyle Cronin Apr 19 '09 at 15:49
  • thanks I've written intersect and union but I've really not know how to call and write a new list I got it thanks.. –  Apr 19 '09 at 16:25
  • 2
    And someday, when these people are your co-workers? It isn't about cheating, it's about helping people get an education in how to solve problems without just asking you to do it for them. It's as much fun to help them learn as it is to code for them (and it's _more_ challenging). Just answer it as a "how should I be thinking?" question instead of a "what should I type?" question. For example, see my response to http://stackoverflow.com/questions/623435/project-euler-problem-233/641635#641635 – MarkusQ Apr 19 '09 at 16:32
  • 1
    This solution suffers from many problems: Scheme has `member` and `memq`, an `equal?` check is usually more expected, we use `else` and not `#t` in `cond` expressions, `intersect` and `subtract` are overcomplicated an inefficient, and finally -- if the goal is to return the four results, then they can all be computed in a single loop. – Eli Barzilay Jun 28 '09 at 10:36
3

Sure it is possible. Assuming that you have function to compute the differences, union intersection etc:

 (define (checkResult lis1 list2)
   (list (difference lis1 lis2)
        (union ...
tomjen
  • 3,779
  • 3
  • 29
  • 35
2

Sure it's possible. Here are a couple hints:

  1. what's the result of combining a list and an empty list?
  2. You don't have to do it all at once. Take a piece at a time.
Charlie Martin
  • 110,348
  • 25
  • 193
  • 263
1

On top of Charlie Martin's and tomjen's answers, I have come up with this source:

Union Intersection and Difference

Implementation of the distinct functions can be found with nice explanations.

rpr
  • 3,768
  • 2
  • 22
  • 20
  • In case this is a HW, which quite seems so,sorry if I am not supposed to reveal everything but only provide tips and guidance. – rpr Apr 19 '09 at 14:43