fork download
  1. ; Evaluate your expression in your DSL
  2. ; ------------------------------
  3. ; The Little Lisper 3rd Edition
  4. ; Chapter 7
  5. ; Exercise 9
  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-7-shadows.html
  9. ; http://t...content-available-to-author-only...t.com/2010/06/little-lisper.html
  10. ; ------------------------------
  11. (setf l1 '())
  12. (setf l2 '(3 + (66 6)))
  13. (setf aexp4 5)
  14. ; ------------------------------
  15.  
  16. (setf lexp1 '(AND (OR x y) y))
  17. (setf lexp2 '(AND (NOT y)(OR u v)))
  18. (setf lexp3 '(OR x y))
  19. (setf lexp4 'z)
  20.  
  21. (defun lookup (a lat)
  22. (cond
  23. ((null a) NIL)
  24. ((null lat) NIL)
  25. ((eq a (car (car lat)))
  26. (cdr (car lat)))
  27. (t
  28. (lookup a (cdr lat)))))
  29.  
  30. (defun operator (aexp_)
  31. (car aexp_))
  32.  
  33. (defun 1st-sub-expr (aexp_)
  34. (car (cdr aexp_)))
  35.  
  36. (defun 2nd-sub-expr (aexp_)
  37. (car (cdr (cdr aexp_))))
  38.  
  39. (defun Mlexp (lexp_ alist_)
  40. (cond
  41. ((null lexp_) NIL)
  42. ((null alist_) NIL)
  43. ((atom lexp_)
  44. (cond
  45. ;((eq lexp_ 't) t)
  46. ((= 1 (car(lookup lexp_ alist_))) t)
  47. (t NIL)))
  48. ((eq (operator lexp_) 'AND)
  49. (cond
  50. ((and
  51. (Mlexp (1st-sub-expr lexp_) alist_)
  52. (Mlexp (2nd-sub-expr lexp_) alist_))
  53. 't)
  54. (t NIL)))
  55. ((eq (operator lexp_) 'OR)
  56. (cond
  57. ((or
  58. (Mlexp (1st-sub-expr lexp_) alist_)
  59. (Mlexp (2nd-sub-expr lexp_) alist_))
  60. 't)
  61. (t NIL)))
  62. ((eq (operator lexp_) 'NOT)
  63. (cond
  64. ((not
  65. (Mlexp (1st-sub-expr lexp_) alist_))
  66. 't)
  67. (t NIL)))
  68. (t NIL)))
  69.  
  70. (setf lexp1 '(AND (OR x y) y))
  71.  
  72. (print (Mlexp lexp1 '((x 1)(y 0)(z 0))))
  73. ;NIL false
  74.  
  75. (print (Mlexp lexp2 '((y 0)(u 0)(v 1))))
  76. ;T
  77.  
  78. (print (Mlexp lexp4 '((x 1)(y 0)(z 0))))
  79. ; NIL false
  80.  
Success #stdin #stdout 0s 10600KB
stdin
Standard input is empty
stdout
NIL 
T 
NIL