Clojure: Fibonacci number problems

tags: program  F#  JVM  data structure  Web

What Clojure is? Is a new language? Not bothersome trouble? This is the first language are not enough it?

Yes, each language are generally a little thing, but to such a point to learn something special reason not sufficient.

However, Clojure can be so different, not just another. Let me start directly from the code open practice, see if you think it's worth more Kanji Yan?

One of my favorite example is the Fibonacci row: bunny mature two months after birth, you can regenerate a pair of small rabbits per month. Asked a pair of such rabbits, 100 one hundred months later would become how many pairs? (The rabbit body good enough)

The mathematical expression of it is simple:
F(n) = F(n-1) + F(n-2), F(1)=F(2)=1。

This question is how to use Java to do? Do you want recursion? Then stack in order to prevent problems, hand out the recursive solution?
Clojure answer:

(defn fibonacci []
(map first (iterate (fn [[a b]] [b (+ a b)]) [1 1])))
(nth (fibonacci) 100)


how about it? Simple, right?
You are not a whisper, what is this stuff, hieroglyphics, there readability it?
Yes, you are used to seeing in a C / Java orthodox vision of such a language point of view, really weird to the extreme. However, the lack of readability may not! Let me break it down:

defn line 1 defines a function named Fibonacci, the parameter table is empty. Why did not the parameters? Because Clojure language can define an unlimited number of columns, the return value is just an infinite series of this function!
Fibonacci number also happens on mathematical infinite, clever.
Note the line 2, whether or not feel familiar? Yes, it is almost a mathematical definition of Fibonacci Fibonacci number sequence:
(Fn [[ab]] [b (+ ab)]) is a function that takes an array as a parameter, known as the first element in the array a, the second element is B, the return value of the function is very simple : [b (+ ab)], an array of two elements, one is B, the second is a + b (the language that a + b hey awkward doing written (+ ab), Naozijinshui up? Do not worry, will explain later). This function is essentially seeking next to that number from that deed Fibonacci number Fibonacci deed one pair. We will iterate the function to the function as the first argument, that the number of the first deed of Fibonacci [11] gave it as the second argument. function to iterate the second parameter as an initial value, each call to the first parameter value (function) of the number of columns as the next value of the number of columns. Think about it, call our function parameters return to the initial value of what? Is [12], if we call our function on this value, it is [23].
Obviously, iterate get into a number of columns [11] [12] [23] [35] ... Each element is an array, this is not what we want, so we use it map first , takes the first element of each return value, get the number of columns 1,1,2,3,5,8 ...!

And so, since this is an infinite number of columns, how memory can let go of it? I just 100th, why should it unlimited ah?
Clojure clever at the number listed on this: it is lazy! Only when you are interested in specific elements of its value, it will generate a method to call the elements to obtain the value of the element. So this infinite array, the basic fact is not what memory consumption!
Therefore, our fibonacci function, almost equivalent to the mathematical expression of fibonacci. Is not seem to have had good readability?
Laziness characteristic of the number of columns, the actual calculation occurs on line 3, nth function takes the number of columns of item 100, iterate start running ... seeking to item 100, satisfaction! Calculation stops.

This example is only three lines, however, make it clear that the expression of natural language, really spent a lot of effort. Visible, Clojure highly concise language, if you're like me, do not like wordy instigate code, Clojure will be up your alley. Also, this is not an extreme example, Clojure language almost entirely by the number of columns abstract calculation process, basically you can not see what the cycle, what the loop variable, the elimination of these factors easily lead to errors, so not only more concise, general the quality is also higher.

If you see this, congratulations, you should be a little interested. You might ask, well, the language a bit mean, but at best it is the kind of toy language. Practical value?
Have. Use Clojure completed projects is increasing rapidly, it is generally used in the quality of the most demanding server programming (of course, also includes Web programming, we currently it is being used), for it can improve the quality of your code while improving your work efficiency!
Because it itself is extremely simple and consistent.
If more than 10 readers encouraged me to continue to introduce it, I will then write the second chapter.

Background (if you have not read enough words):
Clojure is a language based on the new dynamic language for the JVM,
It can lead to higher productivity for you, better code quality, programming more enjoyable experience. It can seamlessly use all Java libraries. You can read English materials on clojure.org.

Intelligent Recommendation

Fibonacci number · rabbit reproductive problems (python)

Problem Description Rabbit born three months from the month a small rabbit, rabbit animate been a long month, a small rabbit to three months, if the rabbit is not dead, then from the first month of a ...

Fibonacci number problem, bunny rabbit Health problems

Can be seen that the n-month rabbits number f (n) f (n) = f (n-1) + f (n-2) is a column for Solving the Fibonacci...

Clojure - basic syntax - number type

[b][color=red] First, arithmetic operation[/color][/b] [color=red]addition +:[/color] The addition function (+) accepts arguments of any numeric type and returns their sum; returns 0 if there are no a...

Count the number of sub-problems decomposed when calculating a Fibonacci number

Title description: The divide-and-conquer strategy is one of the important strategies for algorithm design. The basic idea of ​​this strategy is to decompose the problem into some sub-problems, and so...

Fibonacci Number Fibonacci Number

The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is, Given N, calcul...

More Recommendation

Fibonacci Problems

Problem1: Moving several elements from the beginning of an array to the end of the array is called array rotation. Input a rotation of a non-decreasing sorted array, and output the smallest element of...

Clojure

http://xumingming.sinaapp.com/category/storm/ Clojure Learning Primer (1) - Learning Materials Clojure (pronounced "closure", ['kləʊʒə(r)]) is a dynamic version of the modern Lisp language. ...

Exercise 4-11 rabbit breeding problems (Fibonacci number)

One pair of rabbits from the first 3 months after the birth of one pair of rabbits are born every month. Bunnies grow up to 3 months after the month gave birth to one pair of rabbits. If the rabbit is...

C language journey only for circulating seeking Fibonacci number problems

Ⅰ, seeking for loop problems columns Fibonacci numbers: ①, // the code can be run directly off // Fibonacci number problem #include<stdio.h> int main() { int t,s,a1,a2,n; s=0;a1=1;a2=1; printf(&...

Fibonacci number sequence and amount frog jump step problems

  Fibonacci (Fibonacci) the number of columns is defined as follows: Inefficient solution:   Improved algorithm: calculated from the bottom up. The first calculating f f (0) and f (1) (2), a...

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

Top