Skip to main content
add link to question
Source Link

I'm a Clojure novice working through 4Clojure problems. In one problemone problem I am supposed to create a function that takes a number and returns that number of Fibonaccis. My first thought was to create a function that creates a lazy sequence of Fibonaccis and then just takes however many it needs. Here is what I came up with:

(fn [numberOfFibs]
 (defn lazyFib [a b]
   (cons a (lazy-seq (lazyFib b (+ b a)))))
 (take numberOfFibs (lazyFib 1 1)))

This did work in my IDE but it wasn't accepted by 4Clojure due to the private defn. defs are not accepted by the website.

How can I refactor this function to remove the defn? And what are the problems with style and readability? Is it easy to reason about the function?

I'm a Clojure novice working through 4Clojure problems. In one problem I am supposed to create a function that takes a number and returns that number of Fibonaccis. My first thought was to create a function that creates a lazy sequence of Fibonaccis and then just takes however many it needs. Here is what I came up with:

(fn [numberOfFibs]
 (defn lazyFib [a b]
   (cons a (lazy-seq (lazyFib b (+ b a)))))
 (take numberOfFibs (lazyFib 1 1)))

This did work in my IDE but it wasn't accepted by 4Clojure due to the private defn. defs are not accepted by the website.

How can I refactor this function to remove the defn? And what are the problems with style and readability? Is it easy to reason about the function?

I'm a Clojure novice working through 4Clojure problems. In one problem I am supposed to create a function that takes a number and returns that number of Fibonaccis. My first thought was to create a function that creates a lazy sequence of Fibonaccis and then just takes however many it needs. Here is what I came up with:

(fn [numberOfFibs]
 (defn lazyFib [a b]
   (cons a (lazy-seq (lazyFib b (+ b a)))))
 (take numberOfFibs (lazyFib 1 1)))

This did work in my IDE but it wasn't accepted by 4Clojure due to the private defn. defs are not accepted by the website.

How can I refactor this function to remove the defn? And what are the problems with style and readability? Is it easy to reason about the function?

edited tags
Link
RubberDuck
  • 31.2k
  • 6
  • 74
  • 177
Source Link

Clojure Fibonacci

I'm a Clojure novice working through 4Clojure problems. In one problem I am supposed to create a function that takes a number and returns that number of Fibonaccis. My first thought was to create a function that creates a lazy sequence of Fibonaccis and then just takes however many it needs. Here is what I came up with:

(fn [numberOfFibs]
 (defn lazyFib [a b]
   (cons a (lazy-seq (lazyFib b (+ b a)))))
 (take numberOfFibs (lazyFib 1 1)))

This did work in my IDE but it wasn't accepted by 4Clojure due to the private defn. defs are not accepted by the website.

How can I refactor this function to remove the defn? And what are the problems with style and readability? Is it easy to reason about the function?