- ; Expression evaluators - Is this reflexive? 
- ; ------------------------------ 
- ; The Little Lisper 3rd Edition 
- ; Chapter 8 
- ; Exercise 2 
- ; 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 build (a b) 
-   (cons a (cons b '()))) 
-   
- (defun idrel (s) 
-   (cond 
-    ((null s) '()) 
-    (t (cons (build (car s) (car s)) 
-             (idrel (cdr s)))))) 
-   
- (defun domset (rel) 
-   (cond 
-    ((null rel) '()) 
-    (t (makeset (flatten rel '()))))) 
-   
- (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))))) 
-   
- (idrel (domset r1)) 
- ;((A A) (B B)) 
-   
- (idrel (domset r2)) 
- ;((C C)) 
-   
- (idrel (domset r3)) 
- ;((A A) (C C) (B B)) 
-   
- (member? 'a '(a b c)) 
- ;T 
-   
- (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)))))) 
-   
- (first_ '(a b)) 
- ;A 
-   
- (second_ '(c d)) 
- ;D 
-   
- (third_ '(a b c)) 
- ;C 
-   
- (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))) 
-   
- (pair? '(a b)) 
- ;T 
-   
- (pair? 'a) 
- ;NIL false 
-   
- (pair? '(a b c)) 
- ;NIL false 
-   
- (defun rel? (rel) 
-   (cond 
-    ((null rel) t) 
-    ((atom rel) NIL) 
-    ((pair? (car rel)) 
-     (rel? (cdr rel))) 
-    (t NIL))) 
-   
- (rel? '((a b))) 
- ;T 
-   
- (rel? '((a a)(a b))) 
- ;T  
-   
- (rel? '(a b)) 
- ;NIL false 
-   
- (rel? '((a b c))) 
- ;NIL false 
-   
- (rel? '((a b) c)) 
- ;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))) 
-   
- (eq-pair '(a b) '(a b)) 
- ;T 
-   
- (eq-pair '(a a) '(b b)) 
- ;NIL false 
-   
- (eq-pair '(a b c) '(a b c)) 
- ;NIL false 
-   
- (eq-pair '(a a) 'a) 
- ;false 
-   
- (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))))) 
-   
- (member-pair? '(a b) '(a b c)) 
- ;NIL false 
-   
- (member-pair? '(a d) '(a b c)) 
- ;NIL false 
-   
- (member-pair? '(a a) '((a a)(a b))) 
- ;T 
-   
- (member-pair? '(a a) '((a b)(a a))) 
-   
- (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))) 
-   
- (member-rel? '((a a)) '((a b)(a a))) 
- ;T 
-   
- (member-rel? '((a a)) '((a b)(a c))) 
- ;NIL false 
-   
- (member-rel? '((a)) '((a b)(a a))) 
- ;NIL false 
-   
- (member-rel? '((a)) '((a b)(a))) 
- ;NIL false 
-   
- (member-rel? '(a a) '((a b)(a a))) 
- ;NIL false 
-   
- (member-rel? '((a a)) '(a b)) 
- ;NIL false 
-   
- (defun reflexive? (lat) 
-   (cond 
-    ((null lat) NIL) 
-    (t (member-rel? (idrel (domset lat)) lat)))) 
-   
- (domset r1) 
- ; (A B) 
-   
- (idrel (domset r1)) 
- ; ((A A) (B B)) 
-   
- (member-rel? (idrel (domset r1)) r1) 
- ;T  
-   
- (print (reflexive? r1)) 
- ;T 
-   
- (print (reflexive? r2)) 
- ;T 
-   
- (print (reflexive? r3)) 
- ;NIL false 
-