使用Lisp:定义一个获取列表和数字的函数,如果列表中出现数字,则返回true

-1 lisp common-lisp

我是lisp的新手,我有一个问题,我正在尝试在列表中找到该号码,但它无效.我还没有发表回复声明

(defun num (x 'y)
    (if (member x '(y)) 't nil))

(write (num 10 '(5 10 15 20)))
Run Code Online (Sandbox Code Playgroud)

我的输出只输出nil而不是执行功能,我对我做错了很困惑.

sds*_*sds 5

(defun member-p (element list)
  "Return T if the object is present in the list"
  (not (null (member element list))))
Run Code Online (Sandbox Code Playgroud)

not/ null模式等同于(if (member element list) t nil)但更常见.

事实上,你真的不需要这个单独的功能, member足够好.

-p后缀代表断言,比照 integerpupper-case-p.

你的代码

  1. 你不能引用拉姆达列表元素,所以你需要更换defun num (x 'y)defun num (x y)
  2. 你不需要引用 t
  3. 引用'(y)毫无意义,用它替换y.
  4. 你不需要write函数调用,REPL会为你做.

也可以看看