Изменить код - Lisp
Формулировка задачи:
Добрый вечер, имеется код, который считает такое выражение : ((A ∩ B) – C) ∩ D , где A,B,C,D-множества.
Как его переделать, что бы считало (С - (A ∩ B)) ∩ D ?
Буду очень благодарна за помощь!
Листинг программы
- (defun func (a b c d &optional (flag '1) (res '()) (temp b) (temp2 '()))
- (cond
- ((equalp flag '1)
- (cond
- ((equalp a nil) (print res) (func a b c d '2 res res res))
- ((equalp b nil) (func (cdr a) temp c d '1 res temp temp2))
- ((equalp (car a) (car b)) (func (cdr a) temp c d '1 (cons (car b) res) temp temp2))
- (T (func a (cdr b) c d '1 res temp temp2))
- )
- )
- ((equalp flag '2)
- (cond
- ((equalp res nil) (func a b (cdr c) d '2 temp2 temp temp2))
- ((equalp c nil) (print temp2) (func a b c d '3 res d '()))
- ((equalp (car res) (car c))(func a b (cdr c) d '2 temp temp (remove (car c) temp2)))
- (T (func a b c d '2 (cdr res) temp temp2))
- )
- )
- (T
- (cond
- ((equalp res nil) temp2)
- ((equalp d nil) (func a b c temp '3 (cdr res) temp temp2))
- ((equalp (car res) (car d)) (func a b c (cdr d) '3 res temp (cons (car res) temp2)))
- (T (func a b c (cdr d) '3 res temp temp2))
- )
- )
- )
- )
Решение задачи: «Изменить код»
textual
Листинг программы
- (defun пересечь (x y)
- (cond ((null x) nil)
- ((memb (car x) y) (cons (car x) (пересечь (cdr x) y)))
- (t (пересечь (cdr x) y))))
- ==> ПЕРЕСЕЧЬ
- (пересечь '(1 2 3 4 5 6) '(4 5 6 7 8 9))
- ==> (4 5 6)
- (defun отнять (x y)
- (cond ((null x) nil)
- ((memb (car x) y) (отнять (cdr x) y))
- (t (cons (car x) (отнять (cdr x) y)))))
- ==> ОТНЯТЬ
- (отнять '(1 2 3 4 5 6) '(4 5 6 7 8 9))
- ==> (1 2 3)
- (defun задача (a b c d)
- (пересечь (отнять c (пересечь a b)) d))
- ==> ЗАДАЧА
- (задача '(1 2 3 4) '(3 4 5 6) '(1 2 3) '(2 3 4))
- ==> (2)
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д