The difference of 2 sets using lisp in Dr.Racket.
Code:
(define (difference a b)
(cond ((null? a) '()) ;;if first set is empty return empty
set
((member (car a) b) (difference (cdr a) b)) ;;id first element is a
member of second set, continue in rest
(else (cons (car a) (difference (cdr a) b))))) ;;else add that
element difference set and continue
(define (memebr a list) ;;HELPER FUNCTION
(cond ((null? list) #f) ;;If list is empty returns false
((equal? a (car list)) #t) ;;if element is equal to first element
of list returns true
(else (member a (cdr list)))));; else continue in rest of the
list
Snapshot of Code and Output:
Get Answers For Free
Most questions answered within 1 hours.