; Expression evaluators - Is this symmetric?
; ------------------------------
; The Little Lisper 3rd Edition
; Chapter 8
; Exercise 3
; Common Lisp
; http://t...content-available-to-author-only...r.com/thelittlelisper
; http://t...content-available-to-author-only...t.com/2010/06/little-lisper-chapter-8-friends-and.html
; http://t...content-available-to-author-only...t.com/2010/06/little-lisper.html
; ------------------------------
(setf r1 '((a b)(a a)(b b)))
(setf r2 '((c c)))
(setf r3 '((a c)(b c)))
(setf r4 '((a b)(b a)))
(setf f1 '((a 1)(b 2)(c 2)(d 1)))
(setf f2 '())
(setf f3 '((a 2)(b 1)))
(setf f4 '((1 $)(3 *)))
(setf d1 '(a b))
(setf d2 '(c d))
(setf x 'a)
; ------------------------------
(defun member* (a l)
(cond
((null l) NIL)
((atom (car l))
(or
(eq (car l) a)
(member* a (cdr l))))
(t (or
(member* a (car l))
(member* a (cdr l))))))
(member* 'chips '((potato) (chips ((with) fish) (chips))))
;T
(member* 'fries '((potato) (chips ((with) fish) (chips))))
;NIL false
(defun lat? (l)
(cond
((null l) t)
((atom (car l)) (lat? (cdr l)))
(t nil)))
(lat? '(bacon (and eggs)))
;NIL
(lat? '(bacon and eggs))
;T
(defun eqlat? (lat1 lat2)
(cond
((and
(null lat1)
(null lat2)) t)
((null lat1) NIL)
((null lat2) NIL)
((not (lat? lat1)) NIL)
((not (lat? lat2)) NIL)
((eq (car lat1) (car lat2))
(eqlat? (cdr lat1) (cdr lat2)))
(t NIL)))
(eqlat? '(a b) '(a b))
;T
(eqlat? '(a b) '(a a))
;NIL false
(eqlat? '(a b) '(a b c))
;NIL false
(eqlat? '(a b) '(a (b)))
;NIL false
(defun non-atom? (a)
(not (atom a)))
(non-atom? 'a)
;NIL false
(non-atom? '(a))
;T
(defun eqlist? (l1 l2)
(cond
((and (null l1) (null l2)) t)
((or (null l1) (null l2)) NIL)
(t (and
(eq (car l1) (car l2))
(eqlist? (cdr l1) (cdr l2))))))
(eqlist? '((a b)(c d)) '((a b)(c d)))
;T
(eqlist? '((a b)(c d)) '((a b)(c e)))
;NIL false
(defun equal? (s1 s2)
(cond
((and (atom s1) (atom s2))
(eq s1 s2))
((and
(non-atom? s1)
(non-atom? s2))
(eqlist? s1 s2))
(t NIL)))
(equal? 'a 'a)
;T
(equal? '(a) '(a))
;T
(defun member_ (lista listb)
(cond
((null lista) t)
((null listb) NIL)
((atom listb)
(eq lista listb))
((atom lista)
(member* lista listb))
((eqlist? lista listb) t)
(t (or (member_ lista (car listb))
(member_ lista (cdr listb))))))
;(t NIL)))
(member_ '((with) fish) '((potato) (chips ((with) fish) (chips))))
;T
(member_ '(chips) '((potato) (chips ((with) fish) (chips))))
;T
(member_ '((a a)) '((a b)(a a)))
;T
(member_ '((a c)) '((a b)(a a)))
;NIL false
(member_ '(a a) '((a b)(a a)))
;T
(defun subset? (set1 set2)
(cond
((null set1) t)
((member_ (car set1) set2)
(subset? (cdr set1) set2))
(t NIL)))
(subset? '(b) '(c b))
;T
(subset? '((a b)) '((a a)(a b)))
;T
(subset? '((a a)(a b)) '((a a)(a b)(a c)(a d)))
;T
(defun eqset? (set1 set2)
(and
(subset? set1 set2)
(subset? set2 set1)))
(eqset? '((a b)) '((a b)))
;T
(defun build (a b)
(cons a (cons b '())))
(defun revrel (rel)
(cond
((null rel) '())
(t (cons
(build
(second (car rel))
(first (car rel)))
(revrel (cdr rel))))))
(revrel '((a b)(c d)))
;((B A) (D C))
(revrel r1)
;((B A) (A A) (B B))
(defun symmetric? (rel)
(eqset? rel (revrel rel)))
(symmetric? r1)
;NIL false
(symmetric? r2)
;T
(symmetric? f2)
;T
(defun intersect (set1 set2)
(cond
((null set1) '())
((not (member_ (car set1) set2))
(intersect (cdr set1) set2))
(t (cons (car set1)
(intersect (cdr set1) set2)))))
(intersect '((a a)(a b)(b b)) '((a a)(b b)))
;((A A) (B B))
(intersect '(a b) '(a b c))
;(A B)
(defun idrel (s)
(cond
((null s) '())
(t (cons (build (car s) (car s))
(idrel (cdr s))))))
(defun member? (a lat)
(cond
((null lat) NIL)
(t (or
(eq (car lat) a)
(member? a (cdr lat))))))
(defun makeset (lat)
(cond
((null lat) '())
((member? (car lat) (cdr lat))
(makeset (cdr lat)))
(t (cons (car lat)
(makeset (cdr lat))))))
(defun notatom (lat)
(not (atom lat)))
(defun flatten (lat acc)
(cond
((null lat) acc)
((notatom (car lat))
(flatten (car lat) (flatten (cdr lat) acc)))
(t (flatten (cdr lat) (cons (car lat) acc)))))
(defun domset (rel)
(cond
((null rel) '())
(t (makeset (flatten rel '())))))
(defun antisymetric? (rel)
(subset? (intersect (revrel rel) rel) (idrel (domset rel))))
(revrel r1)
;((B A) (A A) (B B))
(intersect (revrel r1) r1)
;((A A) (B B))
(antisymetric? r1)
;T
(antisymetric? r2)
;T
(antisymetric? r4)
;NIL false
(defun asymmetric? (rel)
(null (intersect rel (revrel rel))))
(print (asymmetric? r1))
;NIL false
(print (asymmetric? r2))
;NIL false
(print (asymmetric? r3))
;T
(print (asymmetric? r4))
;NIL false
(print (asymmetric? f1))
;T
(print (asymmetric? f2))
;T
(print (asymmetric? f3))
;T
(print (asymmetric? f4))
;T
; assymetric - contains no matching pairs in the relation
; For all a and b in X, if a is related to b, then b is not related to a.