fork download
  1. ; Add binary operators to your expressions
  2. ; ------------------------------
  3. ; The Little Lisper 3rd Edition
  4. ; Chapter 7
  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-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 aexp1 '(1 + (3 * 4)))
  14. (setf aexp2 '((3 ^ 4) + 5))
  15. (setf aexp3 '(3 * (4 * (5 * 6))))
  16. (setf aexp4 5)
  17. ; ------------------------------
  18.  
  19. (setf lexp1 '(AND (OR x y) y))
  20. (setf lexp2 '(AND (NOT y)(OR u v)))
  21. (setf lexp3 '(OR x y))
  22. (setf lexp4 'z)
  23.  
  24. (defun 1st-sub-expr (aexp_)
  25. (car aexp_))
  26.  
  27. (defun 2nd-sub-expr (aexp_)
  28. (car (cdr (cdr aexp_))))
  29.  
  30. (defun loperator? (a)
  31. (cond
  32. ((null a) NIL)
  33. ((eq a 'OR) t)
  34. ((eq a 'AND) t)
  35. ((eq a 'NOT) t)
  36. (t NIL)))
  37.  
  38. (print (loperator? 'AND))
  39. ;T
  40. (print (loperator? 'bob))
  41. ;NIL (false)
  42.  
  43. (defun lexp? (lexp_)
  44. (cond
  45. ((null lexp_) NIL)
  46. ((atom lexp_) t)
  47. ((loperator? (car lexp_))
  48. (cond
  49. ((eq (car lexp_) 'NOT)
  50. (lexp? (1st-sub-expr lexp_)))
  51. (t
  52. (lexp? (1st-sub-expr lexp_))
  53. (lexp? (2nd-sub-expr lexp_)))))
  54. (t NIL)))
  55.  
  56. (print (lexp? lexp1))
  57. ;T
  58.  
  59. (print (lexp? lexp2))
  60. ;T
  61.  
  62. (print (lexp? lexp3))
  63. ;T
  64.  
  65. (print (lexp? aexp1))
  66. ;NIL (false)
  67.  
  68. (print (lexp? l2))
  69. ;NIL (false)
  70.  
Success #stdin #stdout 0.02s 10544KB
stdin
Standard input is empty
stdout
T 
NIL 
T 
T 
T 
NIL 
NIL