- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在编写一个 lisp 函数,它将在不使用“反向”函数的情况下确定一个单词是否为回文。我是 lisp 的新手,我仍在努力掌握这个概念。每次我测试回文时,该函数都返回 NIL
,知道为什么吗?
我想出的功能。
(defun palindromep (list)
(cond
((null list)t)
(t
(and (equal (first list) (first (rest list)))
(palindromep (butlast (rest list)))))))
代码修改
(defun palindromep (list)
(cond
((null list)t)
(t
(and (equal (first list) (first(last list)))
(palindromep (butlast(rest list)))))))
最佳答案
我怎么看它似乎在一组特殊的回文中起作用,其中有偶数个相同类型的元素。
您需要为一个元素列表返回 t
。 IE。 (空(CDR 列表))
。
您的检查是检查前两个元素是否相同,而不是检查第一个元素和最后一个元素是否相同。
编辑
我能想到的使用递归而不使用反向的最佳方法是这样的:
(defun palindromep (list)
(labels ((aux (history tortoise hare)
(cond ((null hare) (equal tortoise history))
((null (cdr hare)) (equal (cdr tortoise) history))
(t (aux (cons (car tortoise) history)
(cdr tortoise)
(cddr hare))))))
(aux '() list list)))
它的工作原理是有一个额外的游标 hare
迭代距离是 tortoise
的两倍,同时在 history 中累积看到的元素
。由于 cons
从头到尾制作列表,因此历史是所有看到的元素的倒序,因此当您到达中间时应该匹配尾部。当 hare 的 cdr
或 cddr
为 null 时,您位于中间,可以通过简单的比较确定回文。
编辑 2
如果您将助手移出,则更容易跟踪并查看发生了什么:
(defun aux (history tortoise hare)
(cond ((null hare) (equal tortoise history))
((null (cdr hare)) (equal (cdr tortoise) history))
(t (aux (cons (car tortoise) history)
(cdr tortoise)
(cddr hare)))))
(defun palindromep (list)
;; just calls helper
(aux '() list list))
;; trace the helper
(trace aux)
(trace equal) ; you might need to follow instructions to unlock
(palindromep '(1 2 3 3 2 1))
0: (AUX NIL (1 2 3 3 2 1) (1 2 3 3 2 1))
1: (AUX (1) (2 3 3 2 1) (3 3 2 1))
2: (AUX (2 1) (3 3 2 1) (2 1))
3: (AUX (3 2 1) (3 2 1) NIL)
4: (EQUAL (3 2 1) (3 2 1))
4: EQUAL returned T
3: AUX returned T
2: AUX returned T
1: AUX returned T
0: AUX returned T
==> T
(palindromep '(1 2 3 4 5 6))
0: (AUX NIL (1 2 3 4 5 6) (1 2 3 4 5 6))
1: (AUX (1) (2 3 4 5 6) (3 4 5 6))
2: (AUX (2 1) (3 4 5 6) (5 6))
3: (AUX (3 2 1) (4 5 6) NIL)
4: (EQUAL (4 5 6) (3 2 1))
4: EQUAL returned NIL
3: AUX returned NIL
2: AUX returned NIL
1: AUX returned NIL
0: AUX returned NIL
==> NIL
关于list - 为什么我的 lisp 函数返回 'NIL',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42181933/
你们能帮帮我吗,这是我们的讲师给我们的教程问题,无论我们尝试了多少,我们实际上似乎都无法破解它。请帮忙 ; perform some type/error checking, ; then ca
在 Common Lisp 中编写、编译和测试一个函数,该函数接受一个列表并计算列表中正整数的总数。必须编译然后执行包含函数的 .lisp 文件。在编译该文件后开始传递它,列出要生成的结果的结果,从而
我是 Lisp 初学者,我很难理解为什么下面的代码会给我一个错误。 (dolist (elem '(mapcar mapcon)) (when (fboundp `
我听说 Lisp 可以让你重新定义语言本身,我也试图研究它,但没有任何地方明确的解释。有人有一个简单的例子吗? 最佳答案 Lisp 用户将 Lisp 称为 可编程编程语言 .用于符号计算 - 用符号计
Closed. This question is off-topic. It is not currently accepting answers. Learn more。 想改进这个问题吗Updat
这些是 cons 参数的不同组合的输出。我刚开始学习 lisp。有人可以帮我理解这些吗? Break 80 [81]> (CONS '(A) 'B) ((A) . B) Break 80 [81]>
这个问题不太可能帮助任何 future 的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况有关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visit
我想问一下为什么这个功能不起作用... (defun nenum(ls) (cond ((null ls) nil) ((listp car(ls)) (nenum (rest ls)
如果我有一个原子,例如“a4”,我需要能够将 1 添加到“4”部分以使其成为 a5,但是因为它被认为是一个字符串,所以这是不可能的,所以如果我可以拆分 (a4 ) 到 ((a)(4)) 中,然后我可以
我有一个关于动态构建函数(或类似的东西)的问题。在 Java 中,我可以通过编程将一些 Source 写入字符串,编译该字符串并像函数一样执行它多次。 假设我有一些遗传算法来创建最佳代码以获取 n 个
我是 Common Lisp 的新手,正在学习教程,但无法全神贯注 (equal '(reverse (a b)) '(b a))) 返回零。 非常感谢您的协助。 M. 最佳答案 在 lisp 中引
我有一个使用列表表示的树。例如: (1 ((2 (3)) (3 (2)))) (2 ((1 (3)) (3 (1)))) (3 ((1 (2)) (2 (1)))))` 现在我需要在维护层次结构树的同
在此站点:http://www.gigamonkeys.com/book/practical-a-simple-database.html有如下列出的用户入口函数: (defun prompt-rea
我对 lisp 比较陌生,对在以下上下文中使用嵌套列表的最佳方法很好奇: 所以,我有以下功能: (defun get-p0 (points) (loop for (label x y) in
我正在为 CLOS 类编写一个函数,该函数反转所述类对象的列表元素。 我有一个返回反向列表的方法,但如何让它将对象的列表设置为该列表?我可以在存储列表的函数中有一个实例变量,然后将元素设置为那个吗?或
我知道,严格来说,没有编译语言或解释语言这回事。 但是,一般来说,LISP 是用来编写 Python、bash 脚本、批处理脚本之类的脚本的吗? 还是像 C++、JAVA 和 C# 这样的通用编程语言
在此站点 http://jatha.sourceforge.net/快速函数的示例是通过递归。是不是递归通常比 Lisp 中的迭代更快并且性能更好? 编辑:Lisp 是否比其他语言更优化递归? 最佳答
另一个新手(常见)LISP 问题: 基本上在大多数编程语言中,函数都有一种方法接收对变量的引用而不仅仅是值,即通过引用传递而不是通过值传递。比方说,为了简单起见,我想编写一个 LISP 函数来接收一个
这个问题在这里已经有了答案: How do I find the index of an element in a list in Racket? (3 个答案) 关闭 9 年前。 如果我有这样的列
我在为这个程序生成正确的输出时遇到了一些问题。我的输出几乎是正确的,但缺少一些步骤。我的代码如下: (defun kt (x y m n) ;set the
我是一名优秀的程序员,十分优秀!