Lisp中的线性搜索,数组错误

Lisp中的线性搜索,数组错误,lisp,common-lisp,Lisp,Common Lisp,对输入参数进行简单的线性搜索,不能使用诸如find等内置函数 不幸的是,我找不到很多合适的文档,因为它们要么已经过时,要么大多数都没有涵盖这么简单的问题 了解lisp的技巧非常感谢 (取消搜索(numray x) (defvar i 0) (在numray为i循环 (续) (=x(aref numray i)“数组中存在元素”) ((/=x(aref numray i)“数组中不存在元素”) (t"iddunno"())) ) (setf numray(制作阵列)(10) :初始内容(0 11

对输入参数进行简单的线性搜索,不能使用诸如find等内置函数

不幸的是,我找不到很多合适的文档,因为它们要么已经过时,要么大多数都没有涵盖这么简单的问题

了解lisp的技巧非常感谢

(取消搜索(numray x)
(defvar i 0)
(在numray为i循环
(续)
(=x(aref numray i)“数组中存在元素”)
((/=x(aref numray i)“数组中不存在元素”)
(t"iddunno"()))
) 
(setf numray(制作阵列)(10)
:初始内容(0 11 2 33 44 55 66 7 88 99)))
(defvar x(读取))
(搜索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
    声明它
  • 在迭代形成循环之前,您需要编写一个
    DO
  • 调用
    =
    的括号是错误的
  • 调用
    /=
    的括号是错误的
  • 向量可以很容易地写成#(1 2 3 4 5)
  • 在全局变量周围放置
    *
  • 不要将函数命名为
    search
    ,因为该函数已经内置
  • 中用于列表,在中用于向量
例如:

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,您需要了解的第一件事是根据列表结构缩进代码:

(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
    声明它
  • 在迭代形成循环之前,您需要编写一个
    DO
  • 调用
    =
    的括号是错误的
  • 调用
    /=
    的括号是错误的
  • 向量可以很容易地写成#(1 2 3 4 5)
  • 在全局变量周围放置
    *
  • 不要将函数命名为
    search
    ,因为该函数已经内置
  • 中用于列表,在中用于向量
例如:

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

大家好,欢迎来到SO。这不是典型的论坛网站。我们很乐意在这里提供帮助,有时甚至是做作业(哦,拜托,这是Lisp!有什么比这更好?:),但你能至少问个问题吗?我还建议大家看看我可以问些什么话题?这是对Lisp的基本介绍:您可以从那里下载本书的一个版本。@RSM感谢您的欢迎,很抱歉只是按照提示进行操作——为将来着名!我相信这对那些使用它的人来说是很好的,但我现在只想用火把它烧了嗨,欢迎来到SO。这不是典型的论坛网站。我们很乐意在这里提供帮助,有时甚至是做作业(哦,拜托,这是Lisp!有什么比这更好?:),但你能至少问个问题吗?我还建议大家看看我可以问些什么话题?这是对Lisp的基本介绍:您可以从那里下载本书的一个版本。@RSM感谢您的欢迎,很抱歉只是按照提示进行操作——为将来着名!我相信这对那些使用它的人来说很好,但我现在只想用火把它烧掉,谢谢。我知道有一个非常简单的方法,我看了那么多的网站,无法把它们拼凑在一起。在尝试循环遍历数组时也会不断出错,但我想这是因为我使用的是in而不是cross。谢谢你的参考资料推荐,我找不到太多可读的。谢谢。我知道有一个非常简单的方法,我看了那么多的网站,无法把它们拼凑在一起。在尝试循环遍历数组时也会不断出错,但我想这是因为我使用的是in而不是cross。谢谢你的参考资料推荐,我找不到太多可读的。