fork download
  1. ; Expression evaluators - Is this an identity relation?
  2. ; ------------------------------
  3. ; The Little Lisper 3rd Edition
  4. ; Chapter 8
  5. ; Exercise 1
  6. ; Common Lisp
  7. ; http://t...content-available-to-author-only...r.com/thelittlelisper
  8. ; http://t...content-available-to-author-only...t.com/2010/06/little-lisper-chapter-8-friends-and.html
  9. ; http://t...content-available-to-author-only...t.com/2010/06/little-lisper.html
  10. ; ------------------------------
  11. (setf r1 '((a b)(a a)(b b)))
  12. (setf r2 '((c c)))
  13. (setf r3 '((a c)(b c)))
  14. (setf r4 '((a b)(b a)))
  15. (setf f1 '((a 1)(b 2)(c 2)(d 1)))
  16. (setf f2 '())
  17. (setf f3 '((a 2)(b 1)))
  18. (setf f4 '((1 $)(3 *)))
  19. (setf d1 '(a b))
  20. (setf d2 '(c d))
  21. (setf x 'a)
  22. ; ------------------------------
  23.  
  24. (defun member? (a lat)
  25. (cond
  26. ((null lat) NIL)
  27. (t (or
  28. (eq (car lat) a)
  29. (member? a (cdr lat))))))
  30.  
  31. (defun makeset (lat)
  32. (cond
  33. ((null lat) '())
  34. ((member? (car lat) (cdr lat))
  35. (makeset (cdr lat)))
  36. (t (cons (car lat)
  37. (makeset (cdr lat))))))
  38.  
  39. (makeset '(a b c d e d c b a))
  40. ;(E D C B A)
  41.  
  42. (defun notatom (lat)
  43. (not (atom lat)))
  44.  
  45. (defun flatten (lat acc)
  46. (cond
  47. ((null lat) acc)
  48. ((notatom (car lat))
  49. (flatten (car lat) (flatten (cdr lat) acc)))
  50. (t (flatten (cdr lat) (cons (car lat) acc)))))
  51.  
  52. (flatten '(a b c (d e f)) '())
  53. ;(F E D C B A)
  54.  
  55. (flatten r3 '())
  56. ;(C A C B);
  57.  
  58. (flatten '((a c)(b c)) '())
  59. ;(C A C B)
  60.  
  61. (defun domset (rel)
  62. (cond
  63. ((null rel) '())
  64. (t (makeset (flatten rel '())))))
  65.  
  66. (domset r1)
  67. ;(a b)
  68.  
  69. (domset r2)
  70. ;(c)
  71.  
  72. (domset r3)
  73. ;(A C B)
  74.  
  75. (defun build (a b)
  76. (cons a (cons b '())))
  77.  
  78. (build 'a 'b)
  79. ;(A B)
  80.  
  81. (defun idrel (s)
  82. (cond
  83. ((null s) '())
  84. (t (cons (build (car s) (car s))
  85. (idrel (cdr s))))))
  86.  
  87. (print (idrel '(a b c)))
  88. ;((A A) (B B) (C C))
  89.  
Success #stdin #stdout 0.01s 10600KB
stdin
Standard input is empty
stdout
((A A) (B B) (C C))