如何使用Clojure生成斐波那契数列? [英] How can I generate the Fibonacci sequence using Clojure?

查看:187
本文介绍了如何使用Clojure生成斐波那契数列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(ns src.helloworld)

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

(fibonacci 0 1)

我是函数式编程的新手,开始学习Clojure,因为非常不同于C#。

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.

这是我遇到的错误:

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.

推荐答案

另一方面,我想要一些好的提示,答案解释你的函数调用中的错误。修正后,你应该看看使用循环/ recur,这将做尾调用优化,因此不使用堆栈每个递归调用。这将是很难给一个例子,而没有给你的整个东西虽然: - /这是另一个线程,他们使用递归在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

这篇关于如何使用Clojure生成斐波那契数列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆