How to write a schema function that takes two lists and returns four lists

I have 2 lists of elements '(abc)' (dbf) and you want to find differences, union and intersection in one result. Is it possible? How?

I wrote a member function that checks if the car is in the first list in the second list, but I cannot throw the member into 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)).

+3
source share
4 answers

Like others, all you have to do is create separate functions for calculating the intersection, union and subtraction of 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 examples of 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)))))

: , . , , , , , .

+10

, . , , ..:

 (define (checkResult lis1 list2)
   (list (difference lis1 lis2)
        (union ...
+3

, . :

  • ?
  • . .
+2

tomjen :

.

+1

Source: https://habr.com/ru/post/1706672/


All Articles