; Expression evals - Is your expression partial order?
; ------------------------------
; The Little Lisper 3rd Edition
; Chapter 8
; Exercise 10
; 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 first_ (l)
(cond
((null l) '())
(t (car l))))
(defun second_ (l)
(cond
((null l) '())
(t (car (cdr l)))))
(defun third_ (l)
(cond
((null l) '())
(t (car (cdr (cdr l))))))
(defun pair? (lat)
(cond
((null lat) NIL)
((atom lat) NIL)
((and (and (not (eq (first_ lat) NIL))
(not (eq (second_ lat) NIL))))
(eq (third_ lat) NIL))
(t NIL)))
(defun rel? (rel)
(cond
((null rel) t)
((atom rel) NIL)
((pair? (car rel))
(rel? (cdr rel)))
(t NIL)))
(defun eq-pair (pair-a pair-b)
(cond
((null pair-a) NIL)
((null pair-b) NIL)
((atom pair-a) NIL)
((atom pair-b) NIL)
((not (pair? pair-a)) NIL)
((not (pair? pair-b)) NIL)
((and (eq (first_ pair-a)
(first_ pair-b))
(eq (second_ pair-a)
(second_ pair-b))))
(t NIL)))
(defun member-pair? (pair rel)
(cond
((null pair) t)
((null rel) NIL)
((not(pair? pair)) NIL)
((not(rel? rel)) NIL)
((eq-pair (car rel) pair) t)
(t (member-pair? pair (cdr rel)))))
(defun member-rel? (rel1 rel2)
(cond
((null rel1) t)
((null rel2) NIL)
((not (rel? rel1)) NIL)
((not (rel? rel2)) NIL)
((member-pair? (car rel1) rel2)
(member-rel? (cdr rel1) rel2))
(t NIL)))
(defun build (a b)
(cons a (cons b '())))
(defun idrel (s)
(cond
((null s) '())
(t (cons (build (car s) (car s))
(idrel (cdr s))))))
(defun makeset (lat)
(cond
((null lat) '())
;((member? (car lat) (cdr 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 reflexive? (lat)
(cond
((null lat) NIL)
(t (member-rel? (idrel (domset lat)) lat))))
(defun reflexive? (lat)
(cond
((null lat) NIL)
(t (member-rel? (idrel (domset lat)) lat))))
(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))))))
(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))))))
(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))))))
(defun subset? (set1 set2)
(cond
((null set1) t)
((member_ (car set1) set2)
(subset? (cdr set1) set2))
(t NIL)))
(defun union_ (set1 set2)
(cond
((null set1) set2)
((member_ (car set1) set2)
(union_ (cdr set1) set2))
(t (cons (car set1)
(union_ (cdr set1) set2)))))
(defun rapply (rel x)
(cond
((null rel) '())
((null x) NIL)
((and (rel? rel) (atom x))
(cond
((eq (first_ (car rel)) x)
(cons (second_ (car rel)) (rapply (cdr rel) x)))
(t (rapply (cdr rel) x))))
(t NIL)))
(defun lat? (l)
(cond
((null l) t)
((atom (car l)) (lat? (cdr l)))
(t nil)))
(defun rin (x set)
(cond
((null x) NIL)
((null set) '())
((lat? set)
(cons (build x (car set))
(rin x (cdr set))))
(t NIL)))
(defun rcomp (rel1 rel2)
(cond
((null rel1) '())
(t (union_ ;'union_
(rin
(first_ (car rel1))
(rapply rel2 (second_ (car rel1))))
(rcomp (cdr rel1) rel2)))))
(defun transitive? (rel)
(subset? (rcomp rel rel) rel))
(defun quasi-order? (rel)
(and (reflexive? rel) (transitive? rel)))
(print (quasi-order? r1))
;T
(print (quasi-order? r3))
;NIL false
(defun intersect (set1 set2)
(cond
((null set1) '())
((not (member_ (car set1) set2))
(intersect (cdr set1) set2))
(t (cons (car set1)
(intersect (cdr set1) set2)))))
(defun revrel (rel)
(cond
((null rel) '())
(t (cons
(build
(second_ (car rel));_
(first_ (car rel)));_
(revrel (cdr rel))))))
(defun antisymetric? (rel)
(subset? (intersect (revrel rel) rel) (idrel (domset rel))))
(defun partial-order? (rel)
(and (quasi-order? rel) (antisymetric? rel)))
(print (partial-order? r1))
;T
(print (partial-order? r3))
;NIL false
(defun eqset? (set1 set2)
(and
(subset? set1 set2)
(subset? set2 set1)))
(defun symmetric? (rel)
(eqset? rel (revrel rel)))
(defun equivalence? (rel)
(and (quasi-order? rel) (symmetric? rel)))
(print (equivalence? r1))
;NIL false
(print (equivalence? r2))
;T