; true if predicates p1 and p2 agree on args; false otherwise (defun predicates-agree (p1 p2 args) (let ((p1-result (apply p1 args)) (p2-result (apply p2 args))) (not (or (and p1-result (not p2-result)) (and p2-result (not p1-result)))))) ;; 1.a (princ "1.a") (terpri) (defun model-member? (element set) (member element set :test #'equal)) (loop for args in '((a (a b c)) (b (a b c)) (c (a b c)) (d (a b c)) (a ()) ((a b) ((a b) (b c))) ((a b) ((a d) (a d e) (a b d e) (a b))) (a ((a d) (a d e) (a b d e) (a b)))) do (when (not (predicates-agree #'member? #'model-member? args)) (princ "member? incorrect when applied to ") (princ args) (terpri))) ;; 1.b (princ "1.b") (terpri) (when (not (= 0 (length (union-of () () )))) (princ "failed p1.b test 1") (terpri)) (when (not (= 3 (length (union-of '(a b c) ())))) (princ "failed p1.b test 2") (terpri)) (when (not (= 3 (length (union-of () '(a b c))))) (princ "failed p1.b test 3") (terpri)) (when (not (= 4 (length (union-of '(a b) '(c d))))) (princ "failed p1.b test 4") (terpri)) (when (not (= 3 (length (union-of '(a b c) '(a b c))))) (princ "failed p1.b test 5") (terpri)) (when (not (= 3 (length (union-of '(a b c) '(c b a))))) (princ "failed p1.b test 6") (terpri)) (when (not (= 3 (length (union-of '(a b) '(b c))))) (princ "failed p1.b test 7") (terpri)) (when (not (= 4 (length (union-of '((a d) (a d e) (a b d e)) '((a d e) (a b d e) (a b)))))) (princ "failed p1.b test 8") (terpri)) ;; 2 (princ "2") (terpri) (when (not (equal '() (add-mag-to-each '()))) (princ "failed p2 test 1") (terpri)) (when (not (equal '(2) (add-mag-to-each '(1)))) (princ "failed p2 test 2") (terpri)) (when (not (equal '(4 5) (add-mag-to-each '(1 2)))) (princ "failed p2 test 3") (terpri)) (when (not (equal '(7 8 9) (add-mag-to-each '(1 2 3)))) (princ "failed p2 test 4") (terpri)) (when (not (equal '(11 12 13 14) (add-mag-to-each '(1 2 3 4)))) (princ "failed p2 test 5") (terpri)) ;; 3.a (princ "3.a") (terpri) (defun model-f (n m) (cond ((< m 2) t) ((= 0 (mod n m)) nil) (t (model-f n (- m 1))))) (loop for i from 2 to 10 do (loop for j from 1 to 10 do (when (not (predicates-agree #'f #'model-f (list i j))) (princ "f incorrect when applied to ") (princ (list i j)) (terpri)))) ;; 3.b (princ "3.b") (terpri) (defun model-prime (n) (model-f n (- n 1))) (loop for i from 2 to 100 do (when (not (predicates-agree #'prime #'model-prime (list i))) (princ "prime incorrect when applied to ") (princ (list i)) (terpri)))