Elegant Functional programming language

Source: Internet
Author: User
Tags builtin

Quick Platoon, any algorithm basic tutorial must speak of the last sort of algorithm, I this poor student until graduation also failed to use Java or C write out a quick row. I am vague until he is a "divide and conquer" the idea, but a write to divide and conquer when the brain a piece of paste, do not know where the points. I don't want to memorize the lengthy code. Relive the nightmare and post an online search of the Java implementation of the fast-line:

 Public int Getmiddle(Integer[]List, intLow, intHigh) {intTmp=List[Low]; while (Low<High) { while (Low<High&&List[High] >Tmp) {High--;}List[Low] =List[High]; while (Low<High&&List[Low] <Tmp) {Low++;}List[High] =List[Low];}List[Low] =Tmp;returnLow;} Public void _quicksort(Integer[]List, intLow, intHigh) {if (Low<High) {intMiddle= Getmiddle(List,Low,High);_quicksort(List,Low,Middle- 1);_quicksort(List,Middle+ 1,High);}} Public void Quick(Integer[]Str) {if (Str.Length> 0) {_quicksort(Str, 0,Str.Length- 1);}}
??

OK, this is not the Java algorithm discussion post, it is clear that I paste so much is used to black Java. The concrete for a while then black.

Continue my tragic story, because can't write fast platoon, I do not know how many companies in the written test directly rejected, until I never test the algorithm of small companies have accumulated experience and qiantouming to do the front end. When will I be able to write out the quick line? When you write in a different language.

When I learned Haskell, I saw a quick line, and I frowned, but when I saw the code, I was stunned, and I was surprised not how concise the code was, which I had guessed, and what surprised me was that the idea of a quick line was so simple. On the Haskell code:

QS‘[] = []QS‘(x:XS) = Let Lower = QS‘ . Filter (< x) $ XS;Higher = QS‘ . Filter (>= x) $ XSinch Lower ++ [x] ++ Higher

Five! Of course, I said that surprises me is not the five elements, but I see this code just describes what is a quick row of divide and conquer, as to how the specific divide and conquer seems to be not its focus at all. What does elegance mean? Elegance is someone else in that poor work when you point a cigarette slowly careless took out the results.

These symbols are somewhat simplistic, and I'll explain a little bit to the understanding of those who have not touched Haskell. Qs ' is the function name, followed by the argument convention, after the equal sign is the definition of the function, it can be understood that this is two overloaded functions. Empty lists are returned directly to the empty list; For a list of content, it is decomposed into the head plus tail (head is the first element, the rest is the tail), the following let is a comparison of the small list of elements and the definition of large list, is the QS function and filter function channel together, the parameters of the back of the call, ( <X) is also a function, meaning is very obvious, is to determine whether it is smaller than X, to filter out the conditions, recursive to it continue to implement QS '. Finally, the small list, the median (that is, the head element) and the large list together.

Well, basically it's a description of the algorithm, and there's no way to see what kind of conditions the Java code is moving to, it's the most error-prone thing.

Then see why it can be expressed in such a concise way. As a comparison, look at the implementation of other languages. I write it with Coffeescript, and it's easy to write.

Qs= (List) ->  if !List? || !List.Slice|| !List.LengthreturnList First=List[0]Rest=List.Slice1Lower=QS Rest.Filter(X) ->X<First higher=QS Rest.Filter(X) ->X>=First lower.concat(First).Concat higher

A little more stuff, what's more? Coffee not only need to be sentenced to empty, but also to determine the parameter type, because coffee is a dynamic type language, and then the tail and back need to be taken separately, because coffee is not deconstructed. The parameters on the Haskell Code (X:XS) are actually deconstructed, that is, by the way, the parameters are decomposed into a usable form. The following lines are the same, but coffee has an anonymous function, that is (x)->x<first, although coffee has written the function is very simple, but there is no Haskell written (<first) so elegant, (<x) This form is called incomplete invocation, is the result of the curry (curry over two days, specifically said). Overall coffee is still more elegant. Again with the JS compare:

function QS(List) {  if (!List|| !List.Slice|| !List.Length)    returnList;  varFirst=List[0];  varRest=List.Slice(1);  varSmaller= QS(Rest.Filter(function(X) {    returnX<First;  }));  varBigger= QS(Rest.Filter(function(X) {    returnX>=First;  }));  returnSmaller.concat(First).concat(Bigger);}

JS code is a bit longer. The difference between the grammatical sugars is not said, the main point is that the function needs to display the declaration return value, rather than the default return of the last sentence as coffee. It looks like a multiple return problem, which is actually a "everything is an expression" idea, which is a much closer to the idea of functional programming, because functional programming has no side effects and can only return values, so everything has to be an expression.

In fact, the JS version of its grammatical elements and the overall idea is not much worse than the version of Haskell. However, if I have not seen the version of Haskell, I will certainly follow the Java idea to write, because JS has a functional programming ability to see how you use, it is only extremely flexible, you use what is what, and will be much worse, this is the charm of JS. But there is no good guidance can be used as Java, remember to read a JavaScript design mode of the book, is trying to write JS Java, I did not read. Because of this, a good learning of a good functional programming language for JS development skills should be beneficial.

Go back to Black Java (Java Programmer Please be calm, actually I am also Java programmer origin, not long ago my official position is "Java Development engineer")

Also, look at what's in the Java version. I took a lot of time to finish the code. _quicksort This method is almost the same as Haskell, which is to express the recursive divide and conquer. However, Java seems to be more concerned about saving memory, the entire code does not return a new list, has been in the parameter passed to the list of adjustments, find the middle value of this step is simply ... It's like bubbling. If you do not tinker inside the incoming list, do you filter out the new small list each time to the previous versions of the language? Yes, it's OK. Swap steps are saved, but filter, how to filter? The front ones are the most filter engines that pass in a function (or even the super-simple function that is not called entirely), Java has no intrinsic function (java8 what I don't know), tragedy, or write a loop to a overhangs, or write an inner class ... I remember the scene when I learned to write swing in the inner class ...

Not yet dark, the versions of Haskell, coffee, and JS are functionally equivalent, while the Java version of the functionality is castrated. People sort the object is any list, Java in this version can only order the integer array. Can I use generics and interfaces? I remember there was an interface called compareable ... Well, anyway, I searched on the internet for a long while. No exception is the sort of shape array, this can only show that Java processing common type is very painful, in order to simplify is not bothered to write out.

Haskell is also a strong static type language, why does the code not feel the existence of the type?

Elegance is like this, juchongruoqing.

Elegant Functional programming language

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.