fork download
  1. ; Expression evalutors - Can you evaluate this relation?
  2. ; ------------------------------
  3. ; The Little Lisper 3rd Edition
  4. ; Chapter 8
  5. ; Exercise 6
  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 first_ (l)
  25. (cond
  26. ((null l) '())
  27. (t (car l))))
  28.  
  29. (defun second_ (l)
  30. (cond
  31. ((null l) '())
  32. (t (car (cdr l)))))
  33.  
  34. (defun third_ (l)
  35. (cond
  36. ((null l) '())
  37. (t (car (cdr (cdr l))))))
  38.  
  39. (defun pair? (lat)
  40. (cond
  41. ((null lat) NIL)
  42. ((atom lat) NIL)
  43. ((and (and (not (eq (first_ lat) NIL))
  44. (not (eq (second_ lat) NIL))))
  45. (eq (third_ lat) NIL))
  46. (t NIL)))
  47.  
  48. (defun rel? (rel)
  49. (cond
  50. ((null rel) t)
  51. ((atom rel) NIL)
  52. ((pair? (car rel))
  53. (rel? (cdr rel)))
  54. (t NIL)))
  55.  
  56.  
  57. (defun rapply (rel x)
  58. (cond
  59. ((null rel) '())
  60. ((null x) NIL)
  61. ((and (rel? rel) (atom x))
  62. (cond
  63. ((eq (first_ (car rel)) x)
  64. (cons (second_ (car rel)) (rapply (cdr rel) x)))
  65. (t (rapply (cdr rel) x))))
  66. (t NIL)))
  67.  
  68. (print (rapply '((a 1)(b 2)) 'b))
  69. ;(2)
  70.  
  71. (print (rapply f1 x))
  72. ;(1)
  73.  
  74. (print (rapply r1 x))
  75. ;(b a)
  76.  
  77. (print (rapply f2 x))
  78. ;NIL
  79.  
Success #stdin #stdout 0.01s 10600KB
stdin
Standard input is empty
stdout
(2) 
(1) 
(B A) 
NIL