; Add binary operators to your expressions
; ------------------------------
; The Little Lisper 3rd Edition
; Chapter 7
; Exercise 6
; 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-7-shadows.html
; http://t...content-available-to-author-only...t.com/2010/06/little-lisper.html
; ------------------------------
(setf l1 '())
(setf l2 '(3 + (66 6)))
(setf aexp1 '(1 + (3 * 4)))
(setf aexp2 '((3 ^ 4) + 5))
(setf aexp3 '(3 * (4 * (5 * 6))))
(setf aexp4 5)
; ------------------------------

(setf lexp1 '(AND (OR x y) y))
(setf lexp2 '(AND (NOT y)(OR u v)))
(setf lexp3 '(OR x y))
(setf lexp4 'z)

(defun 1st-sub-expr (aexp_)
  (car aexp_))

(defun 2nd-sub-expr (aexp_)
  (car (cdr (cdr aexp_))))

(defun loperator? (a)
  (cond
   ((null a) NIL)
   ((eq a 'OR) t)
   ((eq a 'AND) t)
   ((eq a 'NOT) t)
   (t NIL)))

(print (loperator? 'AND))
;T
(print (loperator? 'bob))
;NIL (false)

(defun lexp? (lexp_)
  (cond
   ((null lexp_) NIL) 
   ((atom lexp_) t)
   ((loperator? (car lexp_))
    (cond 
     ((eq (car lexp_) 'NOT)
      (lexp? (1st-sub-expr lexp_)))
     (t 
       (lexp? (1st-sub-expr lexp_))
       (lexp? (2nd-sub-expr lexp_)))))
   (t NIL)))

(print (lexp? lexp1))
;T

(print (lexp? lexp2))
;T

(print (lexp? lexp3))
;T

(print (lexp? aexp1))
;NIL (false)

(print (lexp? l2))
;NIL (false)
