gpt4 book ai didi

lisp - Common-LISP - 减法

转载 作者:太空宇宙 更新时间:2023-11-03 18:57:35 30 4
gpt4 key购买 nike

我有这个功能:

(defun test (variable)
(cond
((null variable) nil)
((< (- 12 (other-function variable) 3) 0) 1)
(t (- 12 (other-function variable) 3))
)
)

思路是,如果函数with 3减12的结果小于0,则返回1,否则直接做减法。“其他功能”返回一个数字。当我运行这个函数时,lispworks 卡住了。但是如果我在没有第一个条件的情况下运行函数:

(defun test (variable)
(cond
((null variable) nil)
(t (- 12 (other-function variable) 3))
)
)

它使减法没有任何问题。有人可以帮忙吗?谢谢。

编辑:我用 let 尝试过这种方式:

(defun test (variable)
(let (x (other-function variable))
(cond
((null variable) nil)
((< (- 12 x 3) 0) 1)
(t (- 12 x 3)))
)
)

但我仍然遇到与 lispworks 相同的问题,它死机了。当我在没有以下条件的情况下运行时:

 ((< (- 12 x 3) 0) 1)

此功能正常工作。

最佳答案

除非您提供完整的代码和测试用例,否则无法重现。

CL-USER 1 > (lisp-implementation-type)
"LispWorks"

CL-USER 2 > (defun test (variable)
(cond
((null variable) nil)
((< (- 12 (other-function variable) 3) 0) 1)
(t (- 12 (other-function variable) 3))))
TEST

CL-USER 3 > (defun other-function (foo) (+ foo 1))
OTHER-FUNCTION

CL-USER 4 > (test 5)
3

CL-USER 5 > (test 500)
1

此外:LispWorks 通常不会在出现错误时“卡住”。

关于lisp - Common-LISP - 减法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47611082/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com