用 Lisp 编写递归 GCD

发布于 2024-10-31 17:06:35 字数 256 浏览 0 评论 0原文

编译此函数时,我不断收到随机错误:

(defun gcd (a b)
  (if (= b 0)
      a
      (gcd b mod (a b))))

最常见的是它显示“未定义的函数 a”。所以我想我需要在那个地方返回 a 。这不起作用。我得到了很多 if 语句错误的参数。知道我在这里做错了什么吗? Lisp 新手,到目前为止我们的看法还不一致。

在 Windows 7 上的 CLISP 上运行。

I keep getting random errors when compiling this function:

(defun gcd (a b)
  (if (= b 0)
      a
      (gcd b mod (a b))))

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.

Running on CLISP on Windows 7.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

蓝礼 2024-11-07 17:06:35

在 Lisp 中,函数调用总是*以 '(' 开头,因此该行的

(gcd b mod(a b))

意思是“使用参数 bmod 调用函数 gcd,使用参数 b 调用函数 a 的结果”。

我怀疑你真的想要这样的东西:

(gcd b (mod a b))

*我有一段时间没有使用 Lisp 了所以我对“总是”的看法可能不是100%正确。

In Lisp a function call always* starts with '(', so the line

(gcd b mod(a b))

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))

*I haven't used Lisp for a little while so I might not be 100% correct on the "always".

记忆里有你的影子 2024-11-07 17:06:35

(gcd b mod(ab)) 应该是 (gcd b (mod ab))

(gcd b mod(a b)) should be (gcd b (mod a b))

挽袖吟 2024-11-07 17:06:35

您的 mod 函数调用是错误的。这是我的工作代码:

(defun gcd2(a b)
  (if (= b 0) a (gcd2 b (mod a b))))

You mod function call is wrong. Here's my working code:

(defun gcd2(a b)
  (if (= b 0) a (gcd2 b (mod a b))))
半衬遮猫 2024-11-07 17:06:35

通常编译器可以为您提供更多信息:

使用 LispWorks:

(defun gcd1 (a b)
  (if (= b 0)
      a
      (gcd1 b mod (a b))))


CL-USER 31 > (compile 'gcd1)
;;;*** Warning in GCD1: GCD1 is called with the wrong number of arguments: Got 3 wanted 2
;;;*** Warning in GCD1: MOD assumed special

The following function is undefined:
A which is referenced by GCD1
GCD1

因此您会看到使用错误数量的参数调用 GCD1MOD 被假定为一个变量,并且A 被假定为一个函数。

SBCL:

; in: DEFUN GCD1
;     (GCD1 B MOD (A B))
; 
; caught WARNING:
;   The function was called with three arguments, but wants exactly two.

; in: DEFUN GCD1
;     (A B)
; 
; caught STYLE-WARNING:
;   undefined function: A

;     (GCD1 B MOD (A B))
; 
; caught WARNING:
;   undefined variable: MOD

Usually a compiler can give you more information:

Using LispWorks:

(defun gcd1 (a b)
  (if (= b 0)
      a
      (gcd1 b mod (a b))))


CL-USER 31 > (compile 'gcd1)
;;;*** Warning in GCD1: GCD1 is called with the wrong number of arguments: Got 3 wanted 2
;;;*** Warning in GCD1: MOD assumed special

The following function is undefined:
A which is referenced by GCD1
GCD1

So you see that you call GCD1 with the wrong number of arguments, that MOD is assumed to be a variable and that A is assumed to be a function.

SBCL:

; in: DEFUN GCD1
;     (GCD1 B MOD (A B))
; 
; caught WARNING:
;   The function was called with three arguments, but wants exactly two.

; in: DEFUN GCD1
;     (A B)
; 
; caught STYLE-WARNING:
;   undefined function: A

;     (GCD1 B MOD (A B))
; 
; caught WARNING:
;   undefined variable: MOD
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文