Lisp 在列表中找到原子的位置

Lisp 在列表中找到原子的位置,lisp,common-lisp,lispworks,Lisp,Common Lisp,Lispworks,我有一块带有atom 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 5

我有一块带有atom 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))))
您可以在此处进行测试并查看结果

(打印“位置:”(查找T位置(板)))

结果应该是正确的

(0.9)


board函数尝试调用文本列表,就像调用函数一样。这句话放错地方了

find-t-position
功能没有主体。 如果您添加更多代码和实际问题,您将获得更好的反馈


提示:T在当前行
(车载电路板)
,或者您需要搜索电路板
(cdr电路板)
;经常测试以发现错误。

函数试图调用文本列表,就像调用函数一样。这句话放错地方了

find-t-position
功能没有主体。 如果您添加更多代码和实际问题,您将获得更好的反馈


提示:T在当前行
(车载电路板)
,或者您需要搜索电路板
(cdr电路板)
;经常测试以发现错误。

我在这个问题中找到了答案,它的工作非常完美

(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)))))))
我的函数find-t-position调用函数my-position 使用元素“T”和板,并返回元素“T”在中的位置 名单


你可以看到正确的结果

我在这个问题中找到了答案,它的工作非常完美

(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)))))))
我的函数find-t-position调用函数my-position 使用元素“T”和板,并返回元素“T”在中的位置 名单

您可以看到正确的结果

一些测试:

[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
一些测试:

[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

问题是?祝你的家庭作业好运btw@Sylwester,这不是家庭作业,这是一个项目,我不想写太多代码,只有目标,其余的我做了,我更新了问题,多亏投票支持,问题已经接近了。这仍然不是一个问题。行从0开始,列从1开始。图像中的列从1开始,但项目中的列从1开始0@RenataPSouza听起来您想使用
position
position if
。。。这里是他们在HyperSpec中页面的链接。当然,您的案例是二维的,所以一个简单的应用程序是不够的-但它似乎仍然是正确的尝试方向…问题是?祝你的家庭作业好运btw@Sylwester,这不是家庭作业,这是一个项目,我不想写太多代码,只有目标,其余的我做了,我更新了问题,多亏投票支持,问题已经接近了。这仍然不是一个问题。行从0开始,列从1开始。图像中的列从1开始,但项目中的列从1开始0@RenataPSouza听起来您想使用
position
position if
。。。这里是他们在HyperSpec中页面的链接。当然,你的案例是二维的,所以一个简单的应用程序是不够的-但它似乎仍然是一个正确的尝试方向…谢谢你的回答是好的和正确的,但是变量的使用集合指令,setq,setf,循环,破坏性函数,或者强烈建议不要使用任何有副作用的函数,因为它通常表示技术质量较低,因为它是常见的Lisp Hyperspec。也许你可以帮我解决这个问题,将其设置为0到99,我有列表,我只需要交换或洗牌,请看问题谢谢你的回答是好的和正确的,但是变量的使用集合指令,setq,setf,循环,破坏性函数,或者强烈建议不要使用任何有副作用的函数,因为它通常表示技术质量较低,因为它是常见的Lisp Hyperspec。也许您可以帮助我解决0到99之间的问题,我有列表,我只需要交换或洗牌,请查看问题
[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