fork(2) download
  1. ; Fish and chips - finding the first nested occurrence
  2. ; ------------------------------
  3. ; The Little Lisper 3rd Edition
  4. ; Chapter 6
  5. ; Exercise 5
  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-6-oh-my-gawd-its.html
  9. ; http://t...content-available-to-author-only...t.com/2010/06/little-lisper.html
  10. ; ------------------------------
  11. (setf l1 '((fried potatoes)(baked (fried)) tomatoes))
  12. (setf l2 '(((chili) chili (chili))))
  13. (setf l3 '())
  14. (setf lat1 '(chili and hot))
  15. (setf lat2 '(baked fried))
  16. (setf a 'fried)
  17. ; ------------------------------
  18.  
  19. (defun member* (a l)
  20. (cond
  21. ((null l) NIL)
  22. ((atom (car l))
  23. (or
  24. (eq (car l) a)
  25. (member* a (cdr l))))
  26. (t (or
  27. (member* a (car l))
  28. (member* a (cdr l))))))
  29.  
  30. (print (member* 'chips '((potato) (chips ((with) fish) (chips)))))
  31. ;T
  32.  
  33. (defun notatom (lat)
  34. (not (atom lat)))
  35.  
  36. ;version that starts at the end of the list
  37. (defun member-backwards* (a l oldl)
  38. (cond
  39. ((null l) (member* a oldl))
  40. ((notatom (car l))
  41. (or (member-backwards* a (cdr l) oldl)
  42. (member-backwards* a (car l) oldl)))
  43. (t
  44. (member-backwards* a
  45. (cdr l)
  46. (cons (car l) oldl)))))
  47.  
  48. (print (member-backwards* 'chips '((potato) (chips ((with) fish) (chips))) '()))
  49. ;T
  50.  
  51.  
Success #stdin #stdout 0.02s 10544KB
stdin
Standard input is empty
stdout
T 
T