How can I generate a Fibonacci sequence using Clojure?

(ns src.helloworld)

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

(fibonacci 0 1)

I am new to functional programming and decided to start learning Clojure as it is very different from C #. I would like to expand the horizons.

Here is 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 was never my strong suit, and I never did anything to manipulate such numbers, so I would like to get any guidance you can give.

Please do not give me a complete solution.

Preferably, I would like to get some useful tips and maybe a skeleton on how this should look.

+4
source share
5 answers

. /recur, , , . , : -/ , , clojure: Factorial

+1
(fibonacci (+ b 1) a + b)

fibonacci : (+ b 1), a, + b. fibonacci , .

, , . , fibonacci println. println . , println . , fibonacci .

, , . , clojure , . , recur .

, .

+4

, , . fibonacci:

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

, : - ?

+3

, , . . - . , ( - Clojure). , , - -.

+1

When you're done, you can take a look at the decision of Christophe Grand Fibonacci in the Clojure Program Stu Halloway. This is the most elegant solution I've seen. This is on page 137 in the first edition (since I heard that there is a 2nd edition).

0
source

All Articles