; Curry chicken - How to de-nest this list
; ------------------------------
; The Little Lisper 3rd Edition
; Chapter 5
; Exercise 9
; 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-5-multichapter.html
; http://t...content-available-to-author-only...t.com/2010/06/little-lisper.html
; ------------------------------
(setf x 'comma)
(setf y 'dot)
(setf a 'kiwis)
(setf b 'plums)
(setf lat1 '(bananas kiwis))
(setf lat2 '(peaches apples bananas))
(setf lat3 '(kiwis pears plums bananas cherries))
(setf lat4 '(kiwis mangoes kiwis guavas kiwis))
(setf l1 '((curry) () (chicken) ()))
(setf l2 '((peaches) (and cream)))
(setf l4 '())
; ------------------------------
(print (not (atom (list a))))
;validate that in this implementation a list is not an atom
(defun listlength (lat)
(cond
((null lat) 0)
((atom lat) 0)
(t (+ 1 (listlength (cdr lat))))))
(print (listlength (list 'a 'b 'c 'd 'e)))
(print (listlength '()))
(defun up (l)
(cond
((null l) '())
(t (cond
((eq 1 (listlength (car l)))
(cons (car (car l)) (cdr l)))
(t (cons (car l) (up (cdr l))))))))
(print (up (list 'a 'b (list 'c) 'd 'e)))
;flatten this list with a single atom nested list
(defun multiup (l)
(cond
((null l) '())
(t (cond
((eq 1 (listlength (car l)))
(cons (car (car l)) (multiup (cdr l))))
((eq nil (car l))
(multiup (cdr l)))
(t (cons (car l) (multiup (cdr l))))))))
(print (multiup (list 'a 'b (list 'c) 'd (list 'e))))
(print (multiup '(a b (c)(d)())))
(print (multiup l4))
;NIL
(print (multiup l1))
;(CURRY CHICKEN)
(print (multiup l2))
;(PEACHES (AND CREAM))