Functional programming is difficult, which is why you need to learn it.

Source: Internet
Author: User
Tags ocaml
Document directory
  • It gives you an initial impression of being unfriendly.
  • Novelty
  • Take your pills and find your pharmacist
  • See for reality
  • What you want
  • Supplement

It's strange not that few people use functional programming languages every day. If you program using Scala, Haskell, Erlang, F #, or a lisp dialect, it is very likely that no company will hire you. Most people in this industry use object-oriented programming languages such as Python, Ruby, Java or C #-they are very handy. Yes, you may occasionally use one or two functional language features, such as block, but people will not do functional programming.

However, for many years, we have been taught that functional programming languages are great. I still remember the confusion when I first read the well-known paper on learning the lisp language from the ESR. Perhaps most people are more familiar with Paul Graham's article beating the averages:

With lisp development, our development cycle iterates so fast that sometimes we can replicate the same feature when competitors release their new features at press conferences a day or two. When a reporter reporting the release of a product calls us, our product already has the same features.

Among those who convert to functional programming, the common consideration has always been: learning this new, functional language "is good for you "; it's just like some people suggest that 30 minutes of gym activity every day will "make you healthy. However, this also implies the difficulty and effort required to do so. The LISP Language, unlike Haskell, ocaml, and Scala, is regarded as a well-known and hard-to-learn language and is notorious. Elegant people say this is the embodiment of the "depth & breadth" of the lisp language. Unrefined people say this is "Obscenity", "playing with academics", or simply "unnecessary ". I think its difficulty is related to your familiarity with it, and it is an important indicator: learning such a language will make your programming more efficient and capable.

It gives you an initial impression of being unfriendly.

I started programming when I was 7 years old. In the long and boring suburb summer, I was so angry with my grandfather's computer. I learned basic and used it to draw a ball on the screen. I learned Pascal and used it to write a program that can play music through a PC speaker. When I was about 10 years old, I learned the C language, but I encountered a wall that was far from the past until I went to high school. That is: pointer. Even if I don't calculate these damn pointers, I still encounter numerous failures in writing, reading, learning, and practicing. I destroyed my grandfather's hard drive twice (an accident), and finally re-installed the operating system many times. I failed.

Maybe you have a story similar to me, maybe it's totally different. But I think, almost all people who have learned programming have experienced difficulties. After learning some basic knowledge, we will inevitably encounter some recognized conceptual barriers, such as "Pointers ". Many computer science professors will describe pointers as filters on their courses. If you want to be a good programmer, you must be able to understand the pointer. Few people can easily master them. Most people, including me, need continuous exercises and reference examples to understand what pointers are and why they are important.

This difficult effort is not accidental, but a common phenomenon. Pointer is a very powerful and basic function concept. Learning it can make you a better programmer and make your thinking more visualized. Even if your language does not provide features like pointers, data structures and concepts similar to pointers can be seen everywhere.

Novelty

Once you have learned several languages, all languages start to look very similar. People who know Python may not encounter too many problems when learning Ruby. Those who know Java may be familiar with C. Yes. There are also some surprises. Ruby enthusiasts will be surprised by its comprehension when learning python. Java users will be confused about delegation in C. Or, if you just give a glance, they are all very similar. I can say that if you have never known this, once you have learned a lisp language, you will find that all the lisp variants are very similar.

Some people say that most people are completely overwhelmed when using Haskell or ocaml for the first time. In Haskell,Even semicolons are different from others '.. This is not a syntax problem; Haskell and ML languages are completely based on a different concept and a new Language Paradigm. You need to develop applications in different ways, organize applications in different ways, and expand applications in different ways.

Many of these new concepts have incredible power. In Haskell, monads are just as basic and powerful as pointers (You may have used them without knowing what it is called ). Therefore, unlike learning C # after learning Java, those who want to learn functional languages need to go back and learn more basic concepts before learning. It is like learning the pointer again. And, just like when we first started learning programming, some big concepts seem confusing and frustrating until you conquer (and fail) them.

Take your pills and find your pharmacist

Although hard to learn, I firmly believe that learning these functional programming languages will be good for you professionally. I believe that some people will turn their eyes to heaven when reading this point, and it is hard to imagine that these monoids or monad will be useful for them when using Java or C. For me, I am not surprised to prevent them from learning functional language due to such thinking. They need to learn a new concept that is just as basic as pointers and recursion. They need patience and fighting spirit that only professionals can achieve clear business goals. Few people suffer from setbacks after a plastic age-once and again-or we will all become experts now, right?

There are more complicated things. A lot of language and Algorithm Research is implemented in Functional Languages (especially Haskell ). You can easily be confused by these unfamiliar concepts, such as fan Yan,
Half-finished implements actions, some failed Research -- gets lost. Without clear guidance (for example, a good book written by a pragmatic author), the learning tasks that have been difficult have become even more terrible.

These overlapping complex factors lead to unexpected results: Many people are reluctant to invest time in functional programming learning. It is easy to understand this reluctance. "Why don't I spend my time learning these things for implementation ?" However, this idea also shows that you are never willing to waste time on any new technology (just familiar with it ). In a fast-changing industry like software technology, I don't think this is a correct decision.

See for reality

The most obvious benefit of learning a functional programming language is that you can learn the functional concepts in this type of language. It can help your brain to clearly think about and process some amazing and important concepts. This is not a magic of functional programming; the emergence of various languages and paradigms is to deal with a specific category of issues. The killer of functional programming is the increasing trend of parallel programming and metadata programming in today's world.

For example, we have studied a simplified, locally verbose Google famous mapreduce example. It is incredible to describe this example in a functional way:

mapReducer data partitioner mapper reducer =              let partitions = partitioner data              in reduce reducer (map mapper partitions)

It is easy to make such code support parallel computing or distributed parallel computing (for local parallel computing, many feature packages support "pmap" and "preduce"-you only need to use some simple features of functional languages ). Concepts such as maps, partitions, generators, streams, reductions, folds, and function chaining are similar in various functional programming languages. Therefore, for lisp, Haskell, ocaml, even those familiar with Python and Ruby, languages with node-function language features, can easily understand the essence of these ideas.

Let's take a moment to think about how to use an object-oriented language to clearly describe this architecture in a common object-oriented mode. At least what you need to do is define the Declaration used to describe Mapper and reducer. If you are curious, try to describe a minimal "object-oriented" mapreduce in your preferred object-oriented language. I found that it was very arrogant. If you use a Java-style language, it will look like this:

interface Mapper {   B map(A input); } interface Reducer {   Y reduce(X a, X b); } abstract class MapReduce {   private Mapper mapper;   private Reducer reducer;   public MapReduce(Mapper map, Reducer reduce) {     // ...   }   public run(SeqenceType data) {     // ...   } }

Even if loop logic is not added, this lack of common nouns and verbs in functional patterns makes it difficult to define mapreduce. This definition method is almost funny, but it reminds you of functional concepts. Another good example is how the scala language uses a complete Java fork/join class library to easily integrate it into its own syntax.

What you want

So I encourage any programmer who wants to make progress: consider learning a functional language. Haskell and ocaml are both excellent choices, and F # And Erlang are also quite good. They are hard to learn, but it may be a good thing. Try to figure out the complex concepts you have encountered and see if others are using them, you will achieve ideological breakthroughs when looking for the true intentions of these unfamiliar concepts.

Do notTooCare. Just like anything else that requires your time and energy, it is dangerous to invest too much in functional programming. If you fall into the trap of cognitive ability, your investment will be lost. You will easily forget that there are countless computing models in the world, and you will be more likely to forget how many excellent software doesn't use any functional concept at all.

The learning path will become increasingly difficult, but on the other hand, in your daily programming, you will find more and more important concepts and models that can be used. You will become more and more adaptable to such a compact programming style. You will also have a new understanding of how to become a better software engineer.

Supplement

After reading this article, many proofreaders asked me the same question: "It sounds good, David, but what language should I learn ?" Of course, this is a problem they gave me.

I think if you are a very experienced programmer, the answer to this question is: "select one that meets your needs ". If you need to work on JVM, select Scala or clojure. If you want to quickly develop a large distributed software system, select Erlang. Select Haskell or rcaml if you want a language that has super powerful compiler capabilities. If you want a prototype tool that is more powerful than Ruby or Python, select scheme.

Remember that what we are doing here is for practical skills and self-improvement. If you can spare time to learn this, you can go out of your comfort environment and challenge yourself.

Because I have learned lisp and Erlang and used ocaml for professional work, I decided to study Haskell, which is completely another world. I found that the only way to help me understand this language is to rely on learn you a Haskell and
Real world Haskell are two useful guidance materials. These are very well written and valuable, and can be found online for free. If you want to try Haskell, these books can be used as your treasure map.

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.