SHARE
TWEET

Untitled

a guest Sep 19th, 2019 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (define (my-element? x xs)
  2.   (if (not (null? xs))
  3.       (if (equal? x (car xs))
  4.           (equal? x (car xs))
  5.           (my-element? x (cdr xs)))
  6.       (not (null? xs))))
  7.  
  8. (define (list->set xs)
  9.   (if (null? xs)
  10.       '()
  11.       (if (my-element? (car xs) (cdr xs))
  12.           (list->set (cdr xs))
  13.           (cons (car xs) (list->set (cdr xs))))))
  14.  
  15. (define (set? xs)
  16.   (equal? xs (list->set xs)))
  17.  
  18. (define (union xs ys)
  19.   (list->set (append xs ys)))
  20.  
  21. (define (intersection xs ys)
  22.   (if (null? xs)
  23.       '()
  24.       (if (my-element? (car xs) ys)
  25.           (cons (car xs) (intersection (cdr xs) ys))
  26.           (intersection (cdr xs) ys))))
  27.  
  28. (define (difference xs ys)
  29.   (if (null? xs)
  30.       '()
  31.       (if (my-element? (car xs) ys)
  32.           (difference (cdr xs) ys)
  33.           (cons (car xs) (difference (cdr xs) ys)))))
  34.  
  35. (define (symmetric-difference xs ys)
  36.   (union (difference xs ys) (difference ys xs)))
  37.  
  38. (define (set-eq? xs ys)
  39.   (equal? (difference xs ys) '()))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top