; Expression evaluators - Is this an identity relation?
; ------------------------------
; The Little Lisper 3rd Edition
; Chapter 8
; Exercise 1
; 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 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))))))
(makeset '(a b c d e d c b a))
;(E D C B A)
(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)))))
(flatten '(a b c (d e f)) '())
;(F E D C B A)
(flatten r3 '())
;(C A C B);
(flatten '((a c)(b c)) '())
;(C A C B)
(defun domset (rel)
(cond
((null rel) '())
(t (makeset (flatten rel '())))))
(domset r1)
;(a b)
(domset r2)
;(c)
(domset r3)
;(A C B)
(defun build (a b)
(cons a (cons b '())))
(build 'a 'b)
;(A B)
(defun idrel (s)
(cond
((null s) '())
(t (cons (build (car s) (car s))
(idrel (cdr s))))))
(print (idrel '(a b c)))
;((A A) (B B) (C C))