0

我有这个带有原子 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”所在位置 (ij) 的函数。如果板上没有“T”,则应返回 NIL。

(defun find-T-position (board)

 )

你可以在这里测试并查看结果https://ideone.com/GQIePI

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

结果正确应该是

(0 9)

在此处输入图像描述

4

3 回答 3

1

board函数试图调用一个文字列表,就好像它是一个函数一样。报价放错地方了。

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

提示:要么 T 在当前行(car board),要么你需要搜索板子(cdr board);经常测试以发现错误。

于 2019-11-26T07:20:51.713 回答
1
(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
于 2019-11-27T01:29:13.753 回答
0

我在这个问题Lisp position of nested list element with children中找到了答案,它完美地工作

(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 只需使用元素 'T 和 board 调用函数 my-position 并返回元素 'T 在列表中的位置

(defun find-T-position (board)
     (my-position ('T board))

你可以看到正确的结果https://ideone.com/DOIOoB

于 2019-11-27T01:04:52.493 回答