Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Common lisp Lisp列表包含程序_Common Lisp - Fatal编程技术网

Common lisp Lisp列表包含程序

Common lisp Lisp列表包含程序,common-lisp,Common Lisp,如何制作一个Lisp程序来检查列表中是否有字符、字符串或数字 (列表包含“(1 a 2 d 2 5)”a=>T (列表包含“(1a2d25)”x)=>NIL您可以使用(查找列表中的x),如果x在列表中,则返回x;如果不在列表中,则返回NIL (find 'a '(1 a 2 d 2 5)) ; A (find 'x '(1 a 2 d 2 5)) ; NIL (position 'a '(1 a 2 d 2 5)) ; 1 (position 'x '(1 a 2 d 2 5)) ; NIL

如何制作一个Lisp程序来检查列表中是否有字符、字符串或数字

(列表包含“(1 a 2 d 2 5)”a=>T

(列表包含“(1a2d25)”x)=>NIL
您可以使用
(查找列表中的x)
,如果x在列表中,则返回x;如果不在列表中,则返回NIL

(find 'a '(1 a 2 d 2 5)) ; A
(find 'x '(1 a 2 d 2 5)) ; NIL
(position 'a '(1 a 2 d 2 5)) ; 1
(position 'x '(1 a 2 d 2 5)) ; NIL  

因为这是家庭作业,你的教授可能希望看到你实现一个算法。试试这个:

  • 将列表中的汽车与输入符号进行比较
  • 如果相同,则返回true;你完了
  • 如果为空,则返回false;你完了
  • 使用列表的cdr返回到#1。(此处,暗示车不是空的,也不是比较符号)

  • Greg的解决方案是您应该实现的。但是我想补充一点,如果你没有负责人的话,《小阴谋家》是对这类事情的一个很好的介绍。尝试获取一份副本,甚至只是在谷歌图书中打开预览,搜索“会员?”。他们做了你们期望的事情(也就是说,检查汽车是否相等,如果不相等,在cdr上重复出现),但他们跟踪它并在每一步问你们问题


    这不是一本很长或很贵的书,但一旦你读了它,你就会对如何解决这类问题有一种自然的感觉。它们都可以归结为同一件事,对于列表,这等于询问我们是否已经点击了空列表,如果没有,要么用car做些什么,要么在cdr上重复。

    我建议您使用
    位置
    功能。它返回元素在列表中的位置(第一个位置为0),如果不是,则返回NIL

    (find 'a '(1 a 2 d 2 5)) ; A
    (find 'x '(1 a 2 d 2 5)) ; NIL
    
    (position 'a '(1 a 2 d 2 5)) ; 1
    (position 'x '(1 a 2 d 2 5)) ; NIL  
    


    位置
    优于
    查找
    。您可以知道符号
    'NIL
    是否在列表中

    (position 'NIL '(1 a NIL d 2 5)) ; 2
    (position 'NIL '(1 a 2 d 2 5)) ; NIL
    
    但是,

    (find 'NIL '(1 a NIL d 2 5)) ; NIL
    (find 'NIL '(1 a 2 d 2 5)) ; NIL
    

    因此,使用
    find
    无法区分一种情况和另一种情况。

    到目前为止,您有什么发现?