对输入参数进行简单的线性搜索,不能使用find等内置函数。
不幸的是,我找不到很多合适的文档,因为它们要么已经过时了,大多数都没有这么简单地涵盖问题。
关于理解lisp的提示非常值得赞赏。

(defun search(numray x)
    (defvar i 0)
    (loop for i in numray
        (cond
        ((= x (aref numray i) "Element is present in array")
        ((/= x (aref numray i) "Element is not present in array")
        (t "iddunno")))))
 )

(setf numray (make-array '(10)
:initial-contents '(0 11 2 33 44 55 66 7 88 99)))
(defvar x (read))
(search arr x)


检查输入变量的定义数组陈述它是否存在。

最佳答案

(defun search(numray x)
    (defvar i 0)
    (loop for i in numray
        (cond
        ((= x (aref numray i) "Element is present in array")
        ((/= x (aref numray i) "Element is not present in array")
        (t "iddunno")))))
 )

(setf numray (make-array '(10)
:initial-contents '(0 11 2 33 44 55 66 7 88 99)))
(defvar x (read))
(search arr x)

关于Lisp,首先需要了解的是根据列表结构缩进代码:
(defun search (numray x)
  (defvar i 0)
  (loop for i in numray
        (cond
         ((= x (aref numray i) "Element is present in array")
          ((/= x (aref numray i) "Element is not present in array")
           (t "iddunno")))))
  )

(setf numray (make-array '(10)
                         :initial-contents '(0 11 2 33 44 55 66 7 88 99)))
(defvar x (read))
(search arr x)

下一步:
DEFVAR是全局变量,而不是局部变量
您不需要声明i,因为LOOP声明了它
在aDO
调用LOOP的括号不正确
调用=的括号不正确
向量可以很容易地写成#(1 2 3 4 5)
/=放在全局变量周围
不要将函数命名为*,因为该函数已内置
search适用于列表,使用IN用于向量
例子:
CL-USER 3 > (defun note-num-in-array (vector number)
              (loop for item across vector do
                    (print (if (= number item)
                               "Element is present in array"
                               "Element is not present in array"))))
NOTE-NUM-IN-ARRAY

CL-USER 4 > (note-num-in-array #(1 2 3 1 2 4 5 4 3 2) 2)

"Element is not present in array"
"Element is present in array"
"Element is not present in array"
"Element is not present in array"
"Element is present in array"
"Element is not present in array"
"Element is not present in array"
"Element is not present in array"
"Element is not present in array"
"Element is present in array"
NIL

关于lisp - Lisp中的线性搜索,数组错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56028914/

10-12 07:33
查看更多