fork download
  1. ; your code goes here
  2. (defun board ()
  3. "position of T: i=0 e j=9"
  4. '(
  5. ;; 0 1 2 3 4 5 6 7 8 9
  6. (96 25 54 89 21 8 36 14 41 T) ;; 0
  7. (78 47 56 23 5 NIL 13 12 26 60) ;; 1
  8. (0 27 17 83 34 93 74 52 45 80) ;; 2
  9. (69 9 77 95 55 39 91 73 57 30) ;; 3
  10. (24 15 22 86 1 11 68 79 76 72) ;; 4
  11. (81 48 32 2 64 16 50 37 29 71) ;; 5
  12. (99 51 6 18 53 28 7 63 10 88) ;; 6
  13. (59 42 46 85 90 75 87 43 20 31) ;; 7
  14. (3 61 58 44 65 82 19 4 35 62) ;; 8
  15. (33 70 84 40 66 38 92 67 98 97);; 9
  16. )
  17. )
  18.  
  19. (defun my-position (elm tree &optional (start 0))
  20. "find the generalized position of elm inside tree.
  21. Parameters: elm - element to be found
  22. tree - a list of atoms and lists in which to search the element
  23. start - the tentative position"
  24. (cond ((null tree) nil) ; element not present => nil
  25. ((atom (first tree)) ; if the first element is an atom, then
  26. (if (eql elm (first tree)) ; if equal to element, found
  27. (list start) ; return position start
  28. ;; otherwise, recur on rest of list incrementing the tentative position
  29. (my-position elm (rest tree) (1+ start))))
  30. ;; otherwise, the first element is a list,
  31. ;; try to find it inside, with a recursive call
  32. (t (let ((pos (my-position elm (first tree) 0)))
  33. (if pos ; if not nil the element has been found
  34. (cons start pos) ; return the current position followed by the position inside the list
  35. ; otherwise recur on rest of list incrementing the tentative position
  36. (my-position elm (rest tree) (1+ start)))))))
  37.  
  38.  
  39. (defun find-T-position (board)
  40. (my-position 'T board))
  41.  
  42. (defun show-board (board)
  43. (format T "~%")
  44. (mapcar (lambda (x) (format T " ~A ~%" x)) board)
  45. (format nil "")
  46. )
  47.  
  48. ;;test
  49. (show-board (board));; show board
  50.  
  51. (print "Position of T is:")
  52. (print (find-T-position (board)));; show position of T in list
  53.  
Success #stdin #stdout 0.01s 9656KB
stdin
Standard input is empty
stdout
	(96 25 54 89 21 8 36 14 41 T) 
	(78 47 56 23 5 NIL 13 12 26 60) 
	(0 27 17 83 34 93 74 52 45 80) 
	(69 9 77 95 55 39 91 73 57 30) 
	(24 15 22 86 1 11 68 79 76 72) 
	(81 48 32 2 64 16 50 37 29 71) 
	(99 51 6 18 53 28 7 63 10 88) 
	(59 42 46 85 90 75 87 43 20 31) 
	(3 61 58 44 65 82 19 4 35 62) 
	(33 70 84 40 66 38 92 67 98 97) 

"Position of T is:" 
(0 9)