如何使用 Clojure 生成斐波那契数列?

发布于 2024-11-01 23:18:56 字数 576 浏览 7 评论 0原文

(ns src.helloworld)

(defn fibonacci[a b] (println a b (fibonacci (+ b 1) a + b)))

(fibonacci 0 1)

我是函数式编程新手,决定开始学习 Clojure,因为它与 C#非常不同。我想拓宽我的视野。

这是我得到的错误:

Clojure 1.2.0
java.lang.IllegalArgumentException:
Wrong number of args (4) passed to:
helloworld$fibonacci
(helloworld.clj:0) 1:1 user=>
#<Namespace src.helloworld> 1:2 src.helloworld=>

数学问题从来都不是我的强项,而且我从来没有真正做过任何像这样操纵数字的东西,所以我希望您能提供任何指导。

请不要给我完整的解决方案。

我最好想要一些好的提示,也许还有它应该是什么样子的框架。

(ns src.helloworld)

(defn fibonacci[a b] (println a b (fibonacci (+ b 1) a + b)))

(fibonacci 0 1)

I'm new to Functional Programming and decided to start learning Clojure as it's very different from C#. I'd like to broaden my horizons.

Here's the error I get:

Clojure 1.2.0
java.lang.IllegalArgumentException:
Wrong number of args (4) passed to:
helloworld$fibonacci
(helloworld.clj:0) 1:1 user=>
#<Namespace src.helloworld> 1:2 src.helloworld=>

Math problems never were my strong suit and I never really made anything that manipulated numbers like this, so I would like any guidance you can give.

Please don't give me the entire solution.

Preferably I would like some good hints and maybe a skeleton of how it should look like.

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

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

发布评论

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

评论(5

追风人 2024-11-08 23:18:56
(fibonacci (+ b 1) a + b)

在这里,您使用四个参数调用函数 fibonacci(+ b 1) 的结果、变量 a 的值、函数+ 和变量b 的值。由于斐波那契被定义为仅接受两个参数,因此您会得到这样的错误。

修复此问题后,您将收到堆栈溢出错误,而看不到任何输出。这是因为您将对 fibonacci 的递归调用作为 println 的参数。因此它会在执行println之前尝试执行递归调用。由于递归是无限的,因此永远不会发生对 println 的调用。您应该做的是首先打印数字,然后递归调用fibonacci

一旦执行此操作,程序将打印大量数字,但堆栈最终仍会溢出。这是因为 clojure 不会优化尾调用,因此即使使用尾递归,无限递归也会导致堆栈溢出。为了防止这种情况,请使用 recur 形式而不是正常的递归。

除了这些点之外,当您错误地实现斐波那契数列时,您的程序还会打印错误的数字。

(fibonacci (+ b 1) a + b)

Here you're calling the function fibonacci with four arguments: The result of (+ b 1), the value of the variable a, the function + and the value of the variable b. Since fibonacci was defined to only take two arguments, you get the error you do.

Once you fix this, you'll get a stack overflow error without seeing any output. This is because you put the recursive call to fibonacci as the argument to println. So it will try to execute the recursive call before executing println. Since the recursion is infinite, the call to println will never happen. What you should do is first print the number and then call fibonacci recursively.

Once you do this the programm will print a lot of numbers, but the stack will still overflow eventually. This is because clojure does not optimize away tail-calls, so even when using tail recursion, infinite recursion will cause a stack overflow. To prevent this use the recur form instead of normal recursion.

In addition to these points, your program will print the wrong numbers as you implemented the Fibonacci sequence wrongly.

执妄 2024-11-08 23:18:56

以下是有关您为何出现错误的提示。您将以下四个参数传递给 fibonacci

  • (+ b 1)
  • a
  • +
  • b代码>.

这仍然不会给你一个工作函数,这里有一个提示:什么会导致代码停止执行并向用户返回一个值?

Here is a hint about why you got the error you did. You passed the following four arguments to fibonacci:

  • (+ b 1)
  • a
  • +
  • b.

That still won't give you a working function, and here's one hint about that: what will ever cause the code to stop executing and return a value to the user?

半世晨晓 2024-11-08 23:18:56

其他答案向您解释了函数调用中的错误。修复此问题后,您应该考虑使用循环/递归,它将进行尾调用优化,因此不会对每个递归调用使用堆栈。如果不给你整个事情,就很难给出一个例子:-/这是另一个线程,他们在 clojure 中使用递归计算阶乘: 阶乘

The other answers explain to you the error in your function call. After fixing that you should look into using loop/recur which will do tail call optimization and thus not use the stack for each recursive call. It would be pretty hard to give an example without giving you the whole thing though :-/ Here is another thread where they calculate a factorial using recursion in clojure: Factorial

[浮城] 2024-11-08 23:18:56

当我通过一些类似的代码 katas 来学习时,我发现这个网站非常有帮助。
编码 Kata。他们的 fib 序列就像黄带型一样。他们还提供了多种语言的解决方案(Clojure 就是其中之一)。这样,您就可以将您的答案与其他人进行比较,看看您是否可以找到任何提示或更好的做事方法。

When I was going through some similar code katas to learn, I found this site very helpful.
Coding Kata. They have the fib sequence as a yellow-belt kata. They also have solutions posted from people in multiple languages (Clojure is one of them). That way, you can compare your answer to others and see if you can pick up any tips or better way of doing things.

凹づ凸ル 2024-11-08 23:18:56

完成后,您可能希望查看 Stu Halloway 编写的《Programming Clojure》中的 Christophe Grand 斐波那契解决方案。这是我见过的最优雅的解决方案。它位于第一版的第 137 页(因为我听说即将推出第二版)。

When you are done, you may wish to look at Christophe Grand's Fibonacci solution in Programming Clojure by Stu Halloway. It is the most elegant solution I have seen. It is on page 137 in the 1st edition (since I've heard there is a 2nd edition coming).

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