Brain Hole Time: why there are so many programming languages in the world, that is decadent capitalism in order to increase the majority of people's learning costs and the compilation of the principle is too popular, it is recommended that the University of all courses to eliminate the compiling principle, and swing the razor, all the programming language kicked, In addition to machine language, only two languages are left: Assembler and Haskell (escape Concise write a little Haskell fun first of all, some basic operations, command line Press GHCI Enter, some basic operations with a colon, using vim should be familiar with the person, such as: Quit quit: T shows the type of the variable (Char,int and the like): L link outside the written function file, in fact, this mode can not write their own functions, must be written outside the link in order to use first very exciting is the list, The list and the list in Python, or the array in other languages, are a bit like, enclosed in parentheses, such as [1,5,2,2] ["Xin", "Ru", "Zhang"] and then Python, and Haskell can guess, like [1..5 ] means [1,2,3,4,5] [1,3..9] means [1,3,5,7,9] the first two elements and the last element, Haskell can automatically determine and fill the step, in addition to the lazy evaluation of Haskell is different from the place of Python such as I can not write the last element [ 1.] [1,2,3,4,5,........] All natural numbers, its number of elements is infinite, but Haskell will not be silly to put the infinite number in memory, but put it regardless of it, until the time to use the calculation then is a list of a big kill device, you let a high school students write a positive odd set, then may be written: {2*k-1| k >=1} If you write in C or something like this, you will generally write: for (int i = 1;i <= maxn; i++) Oddarray[i] = 2 * I-1; And then if you write with Haskell it can be like this [2 * k-1 | k <-[1 ...]] woc!!! You can also add restrictions such as looking for an odd number of 7 to 1, such as: [2 * k-1 | k <-[1 ...], (2 * k-1) ' mod ' 7 = = 1] Compared to other writing is really low! The second thing is, Haskell does not cycle! Haskell has no loops! Haskell has no loops! The important thing to say three times. In fact, functional programming isBased on the lambda calculus equivalent to the Turing, so please don't think of it as a creature of this PC! So it can only rely on Meng Meng return processing, to give a chestnut, ask for a list of the largest element, let A = [1,5,6,2], of course, there is a system-provided function maximum, direct maximun a can, but how to build this wheel, can be written like this: Max ' [x] = Xmax ' (x:xs) |x > Max ' xs = x|otherwise = Max ' Xs, anyway Haskell will match from top to bottom, so if recursion is above then there is a magical left recursion to appear ... Max ' is a function named Haskell that allows the function name to be added to differentiate between functions that are only nearly as good as the max1,max2,max3...maxinf. The maximum value is itself when there is only one element in the list. Otherwise, it is equal to the maximum value of the remaining element in the first element and array, translated into C should look like this: int max (std::vector<int> Arr, int k)//array arr[] k element start to the last element's maximum value {if (k + 1 = = Arr.size ()) return arr[k];int Leftmax = max (ARR, k+1), if (Leftmax > Arr[k]) return leftmax;else return arr[k];} Similarly, the sum of all the element in the list can be written as ' [x] = Xsum ' (x:xs) = x + Sum ' xs If the above so much nonsense is HelloWorld level in half an hour to see the book can learn something, then see this thing is really let me feel that there is a record necessary things, quick Platoon! Sort ' []=[]sort ' [x] = [X]sort ' (x:xs) =sort ' [y|y<-xs,y<=x]++[x] + + sort ' [y|y<-xs,y>x] This posture of the fast row beautiful burst, ah, those c++2min 1min knocked out of the hand speed of the dog door fast cry Ah!! There is wood there!! Contact Haskell two hours, the impression is recursive writing things this goods write is very beautiful!! |