用Lisp编写递归GCD [英] Writing recursive GCD in Lisp
问题描述
在编译此函数时,我总是收到随机错误:
I keep getting random errors when compiling this function:
(defun gcd (a b)
(if (= b 0)
a
(gcd b mod (a b))))
最常见的是说未定义的函数a".所以我想我需要在那个地方返回a.这没有用.对于if语句错误,我得到了很多参数.知道我在这里做错了吗? Lisp的新手,到目前为止,我们还没有见面.
The most common is that it says "undefined function a." So I figured I needed return a in that place. This did not work. I get a to many parameters for if statement error. Any idea what I am doing wrong here? New to Lisp and so far we are not seeing eye to eye.
在Windows 7的CLISP上运行.
Running on CLISP on Windows 7.
推荐答案
在Lisp中,函数调用始终*以'('开头,因此该行
In Lisp a function call always* starts with '(', so the line
(gcd b mod(a b))
表示使用参数b
,mod
调用函数gcd
以及使用参数b
调用函数a
的结果."
means "call the function gcd
with arguments b
, mod
and the result of calling function a
with argument b
".
我怀疑您真的想要这样的东西:
I suspect you really want something like:
(gcd b (mod a b))
*我已经有一段时间没有使用Lisp了,所以我对总是"的理解可能不是100%正确.
*I haven't used Lisp for a little while so I might not be 100% correct on the "always".
这篇关于用Lisp编写递归GCD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!