fork download
  1. ; Curry chicken - How to de-nest this list
  2. ; ------------------------------
  3. ; The Little Lisper 3rd Edition
  4. ; Chapter 5
  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-5-multichapter.html
  9. ; http://t...content-available-to-author-only...t.com/2010/06/little-lisper.html
  10. ; ------------------------------
  11. (setf x 'comma)
  12. (setf y 'dot)
  13. (setf a 'kiwis)
  14. (setf b 'plums)
  15. (setf lat1 '(bananas kiwis))
  16. (setf lat2 '(peaches apples bananas))
  17. (setf lat3 '(kiwis pears plums bananas cherries))
  18. (setf lat4 '(kiwis mangoes kiwis guavas kiwis))
  19. (setf l1 '((curry) () (chicken) ()))
  20. (setf l2 '((peaches) (and cream)))
  21. (setf l4 '())
  22. ; ------------------------------
  23.  
  24. (print (not (atom (list a))))
  25. ;validate that in this implementation a list is not an atom
  26.  
  27. (defun listlength (lat)
  28. (cond
  29. ((null lat) 0)
  30. ((atom lat) 0)
  31. (t (+ 1 (listlength (cdr lat))))))
  32.  
  33. (print (listlength (list 'a 'b 'c 'd 'e)))
  34. (print (listlength '()))
  35.  
  36.  
  37. (defun up (l)
  38. (cond
  39. ((null l) '())
  40. (t (cond
  41. ((eq 1 (listlength (car l)))
  42. (cons (car (car l)) (cdr l)))
  43. (t (cons (car l) (up (cdr l))))))))
  44.  
  45. (print (up (list 'a 'b (list 'c) 'd 'e)))
  46. ;flatten this list with a single atom nested list
  47.  
  48. (defun multiup (l)
  49. (cond
  50. ((null l) '())
  51. (t (cond
  52. ((eq 1 (listlength (car l)))
  53. (cons (car (car l)) (multiup (cdr l))))
  54. ((eq nil (car l))
  55. (multiup (cdr l)))
  56. (t (cons (car l) (multiup (cdr l))))))))
  57.  
  58. (print (multiup (list 'a 'b (list 'c) 'd (list 'e))))
  59. (print (multiup '(a b (c)(d)())))
  60.  
  61. (print (multiup l4))
  62. ;NIL
  63. (print (multiup l1))
  64. ;(CURRY CHICKEN)
  65. (print (multiup l2))
  66. ;(PEACHES (AND CREAM))
  67.  
Success #stdin #stdout 0s 10600KB
stdin
Standard input is empty
stdout
T 
5 
0 
(A B C D E) 
(A B C D E) 
(A B C D) 
NIL 
(CURRY CHICKEN) 
(PEACHES (AND CREAM))