clojure Fibonacci analytical column a solution of

fib problem recently made a sequence of http://www.4clojure.com/problem/26 4Clojure

They used the


#(take % (map first (iterate (fn [[a b]] [b (+ a b)]) [1 1])))


After seeing a finished answer it is used in this way

#((apply comp (repeat (- % 2) (fn [x] (conj x (+ (peek x) (peek (pop x))))))) [1 1])


Read a long time came to understand how it is down here recorded


Here the code apart to similar


(def compute (fn [x] (conj x (+ (peek x) (peek (pop x))))))
(def fn-seq (repeat (- n 2) compute)
(def fib (apply comp fn-seq))
(fib [1 1])



First of all

(fn [x] (conj x (+ (peek x) (peek (pop x)))))

This end view of a single function is better understood, that the tail of the vector adding two records, x is added to the


(repeat (- % 2) (fn [x] (conj x (+ (peek x) (peek (pop x))))))

Repeatedly generating the (- 2%) that function defined later times, the same returns a seq, fn


(apply comp fn-seq)

Is to fn-seq as an argument, call the comp, to generate new fn, fn this role is kept calling the fn compute defined above

Finally, call fn, passing parameters [11]
When n = 3 when
Will (compute [1 1]) => [1 1 2]
when n = 4
(compute (compute [1 1])) => [1 1 2 3]

But this solution still has a bug
n <= 1 when the return or [11]
Anti-repeat back empty seq ()

Looked comp source code, will return identity parameter is null
This return directly [11]

Intelligent Recommendation

java column Fibonacci series

******public class d1t { /** * @param args */ public static void main(String[] args) { /*Scanner scanner=new Scanner(System.in); int n=scanner.nextInt();*/ int[] a=new int[40]; a[0]=0; a[1]=1; for(int...

Fibonacci column deformation

Fibonacci column deformation title Even if they have played blast The meaning of problems Fibonacci number generation rules f (n) = f (n-1) + f (n-2). before the two input Fibonacci numbers of Number ...

Fibonacci column Summary

About Fibonacci number, I believe it is no stranger to the subject on which there are also many. I now summarize some interesting properties about it. Basic issue 1. seek Fibonacci column item k Conve...

Fibonacci column C ++ implementation

Thinking First, the use of mating cycles three variables: Second, the use2With variable cycle:  ...

Fibonacci column 10

We all know that Fibonacci number, and now asked to enter an integer n, you output the n-th Fibonacci number Fibonacci sequence of item (from 0, the first 0 is 0). Fibonacci Recursive issues are class...

More Recommendation

Achieve Fibonacci column with iterators

The definition of a class Fibs So used   Reproduced in: https: //my.oschina.net/u/3243928/blog/890295...

Fibonacci Series title column

One: the original title We all know that Fibonacci number, and now asked to enter an integer n, you output the n-th Fibonacci number Fibonacci sequence of item (from 0, the first 0 is 0). n <= 39 I...

Fibonacci column C language

Fibonacci number (Fibonacci) Fibonacci number (Italian: Successione di Fibonacci), also known as golden series, West Fibonacci series, Feibonaqi number, Fibonacci series, refers to such a series: 0,1,...

Question 7 "Fibonacci column"

Title Description We all know that Fibonacci number, and now asked to enter an integer n, you output the n-th Fibonacci number Fibonacci sequence of item (from 0, the first 0 is 0). n<=39 Time limi...

Fibonacci Column (FIB)

Title: Seeking the Nth item of the Fiboaccium number (FIB) Idea: The law is the first two and for the third item, set the previous one, the first two variables and current variables, the top two varia...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top