查找原子在列表中的位置
find the position of an atom in list
我有这个带有原子 T 的板,我想得到它在列表和子列表中的位置
(defun board ()
"position of T: i=0 e j=9"
'(
;; 0 1 2 3 4 5 6 7 8 9
(96 25 54 89 21 8 36 14 41 T) ;; 0
(78 47 56 23 5 NIL 13 12 26 60) ;; 1
(0 27 17 83 34 93 74 52 45 80) ;; 2
(69 9 77 95 55 39 91 73 57 30) ;; 3
(24 15 22 86 1 11 68 79 76 72) ;; 4
(81 48 32 2 64 16 50 37 29 71) ;; 5
(99 51 6 18 53 28 7 63 10 88) ;; 6
(59 42 46 85 90 75 87 43 20 31) ;; 7
(3 61 58 44 65 82 19 4 35 62) ;; 8
(33 70 84 40 66 38 92 67 98 97);; 9
)
)
从电路板获取线和单元格的函数
(defun line (x board)
(nth x board))
(defun cell-board (x y board)
(nth y (line x board)))
(defun column (index board)
(cond ((not (numberp index)) nil)
((< index 0) nil)
(t (mapcar #'(lambda (line &aux (n-column (nth index line))) n-column) board))))
接收棋盘的函数和returns"T"所在的位置(i j)。如果 "T" 不在棋盘上,则应返回 NIL。
(defun find-T-position (board)
)
你可以在这里测试并查看结果https://ideone.com/GQIePI
(print "position : " (find-T-position (board)))
正确的结果应该是
(0 9)
board
函数尝试调用文字列表,就好像它是一个函数一样。引用放错地方了。
find-t-position
函数没有函数体。
如果您添加更多代码和实际问题,您将获得更好的反馈。
提示:要么T在当前行(car board)
,要么你需要搜索棋盘(cdr board)
;经常测试以发现错误。
我在这个问题中找到了答案,而且效果很好
(defun my-position (elm tree &optional (start 0))
"find the generalized position of elm inside tree.
Parameters: elm - element to be found
tree - a list of atoms and lists in which to search the element
start - the tentative position"
(cond ((null tree) nil) ; element not present => nil
((atom (first tree)) ; if the first element is an atom, then
(if (eql elm (first tree)) ; if equal to element, found
(list start) ; return position start
;; otherwise, recur on rest of list incrementing the tentative position
(my-position elm (rest tree) (1+ start))))
;; otherwise, the first element is a list,
;; try to find it inside, with a recursive call
(t (let ((pos (my-position elm (first tree) 0)))
(if pos ; if not nil the element has been found
(cons start pos) ; return the current position followed by the position inside the list
; otherwise recur on rest of list incrementing the tentative position
(my-position elm (rest tree) (1+ start)))))))
and my function find-t-position just call the function my-position
with element 'T and the board and return the position of element 'T in
list
(defun find-T-position (board)
(my-position ('T board))
你可以看到正确的结果https://ideone.com/DOIOoB
(defun find-t (rows)
(let* ((col nil)
(row (position-if (lambda (r) (setf col (position t r))) rows)))
(values row col)))
一些测试:
[1]> (find-t nil)
NIL ;
NIL
[2]> (find-t '(()))
NIL ;
NIL
[3]> (find-t '((0)))
NIL ;
NIL
[4]> (find-t '((t)))
0 ;
0
[5]> (find-t '((0 t)))
0 ;
1
[6]> (find-t '((0 t 0)))
0 ;
1
[7]> (find-t '((0 0 t)))
0 ;
2
[8]> (find-t '((0 0 0)))
NIL ;
NIL
[9]> (find-t '((0 0 0)
(t 0 0)))
1 ;
0
[10]> (find-t '((0 0 0)
(t 0 t)))
1 ;
0
[11]> (find-t '((0 0 0)
(0 0 t)))
1 ;
2
[12]> (find-t '((0 0 t)
(0 0 t)))
0 ;
2
我有这个带有原子 T 的板,我想得到它在列表和子列表中的位置
(defun board ()
"position of T: i=0 e j=9"
'(
;; 0 1 2 3 4 5 6 7 8 9
(96 25 54 89 21 8 36 14 41 T) ;; 0
(78 47 56 23 5 NIL 13 12 26 60) ;; 1
(0 27 17 83 34 93 74 52 45 80) ;; 2
(69 9 77 95 55 39 91 73 57 30) ;; 3
(24 15 22 86 1 11 68 79 76 72) ;; 4
(81 48 32 2 64 16 50 37 29 71) ;; 5
(99 51 6 18 53 28 7 63 10 88) ;; 6
(59 42 46 85 90 75 87 43 20 31) ;; 7
(3 61 58 44 65 82 19 4 35 62) ;; 8
(33 70 84 40 66 38 92 67 98 97);; 9
)
)
从电路板获取线和单元格的函数
(defun line (x board)
(nth x board))
(defun cell-board (x y board)
(nth y (line x board)))
(defun column (index board)
(cond ((not (numberp index)) nil)
((< index 0) nil)
(t (mapcar #'(lambda (line &aux (n-column (nth index line))) n-column) board))))
接收棋盘的函数和returns"T"所在的位置(i j)。如果 "T" 不在棋盘上,则应返回 NIL。
(defun find-T-position (board)
)
你可以在这里测试并查看结果https://ideone.com/GQIePI
(print "position : " (find-T-position (board)))
正确的结果应该是
(0 9)
board
函数尝试调用文字列表,就好像它是一个函数一样。引用放错地方了。
find-t-position
函数没有函数体。
如果您添加更多代码和实际问题,您将获得更好的反馈。
提示:要么T在当前行(car board)
,要么你需要搜索棋盘(cdr board)
;经常测试以发现错误。
我在这个问题中找到了答案
(defun my-position (elm tree &optional (start 0))
"find the generalized position of elm inside tree.
Parameters: elm - element to be found
tree - a list of atoms and lists in which to search the element
start - the tentative position"
(cond ((null tree) nil) ; element not present => nil
((atom (first tree)) ; if the first element is an atom, then
(if (eql elm (first tree)) ; if equal to element, found
(list start) ; return position start
;; otherwise, recur on rest of list incrementing the tentative position
(my-position elm (rest tree) (1+ start))))
;; otherwise, the first element is a list,
;; try to find it inside, with a recursive call
(t (let ((pos (my-position elm (first tree) 0)))
(if pos ; if not nil the element has been found
(cons start pos) ; return the current position followed by the position inside the list
; otherwise recur on rest of list incrementing the tentative position
(my-position elm (rest tree) (1+ start)))))))
and my function find-t-position just call the function my-position with element 'T and the board and return the position of element 'T in list
(defun find-T-position (board)
(my-position ('T board))
你可以看到正确的结果https://ideone.com/DOIOoB
(defun find-t (rows)
(let* ((col nil)
(row (position-if (lambda (r) (setf col (position t r))) rows)))
(values row col)))
一些测试:
[1]> (find-t nil)
NIL ;
NIL
[2]> (find-t '(()))
NIL ;
NIL
[3]> (find-t '((0)))
NIL ;
NIL
[4]> (find-t '((t)))
0 ;
0
[5]> (find-t '((0 t)))
0 ;
1
[6]> (find-t '((0 t 0)))
0 ;
1
[7]> (find-t '((0 0 t)))
0 ;
2
[8]> (find-t '((0 0 0)))
NIL ;
NIL
[9]> (find-t '((0 0 0)
(t 0 0)))
1 ;
0
[10]> (find-t '((0 0 0)
(t 0 t)))
1 ;
0
[11]> (find-t '((0 0 0)
(0 0 t)))
1 ;
2
[12]> (find-t '((0 0 t)
(0 0 t)))
0 ;
2