Sth about functional programming (functional programming)

Source: Internet
Author: User

Today's meeting referred to functional programming, for different types of Centennial City approach, access to a portion of the information, shown as follows:

Programming language has been to modern times, from the Assembly to C to Java, are standing in the computer's perspective, considering the operating mode and operating efficiency of the CPU, in order to design an efficient programming language, as a bridge between human and computer communication. Because the computer is essentially serially executing a flow of instructions, the programming language is also designed for imperative programming (imperative programming), what counts first, how the input calculates how to output, all by the programmer.

Later, we found that the von Neumann structure of the idea of the equalization of data and instructions can help us to better partition the program segment, to achieve modularity, and thus slowly appear object-oriented programming (object-oriented programming), The data and instruction are treated as a small whole, and the data coupling degree of large programs is greatly reduced through inheritance, polymorphism, encapsulation and other characteristics.

And now, with the tireless efforts of the elites of all walks of life, Moore's law has been dogged for decades and is now exhausted. Therefore, the computer began to cluster development, the emergence of big data, cloud computing, heterogeneous parallelism and many other new areas. The transformation of the underlying architecture is also bound to require a new exploration of the upper programming language, the Lisp language, the math department of the birth of Donglin old man, once again to regain new life, is the concern of everyone. But in fact, even now, our computing power is not extravagant enough to be able to use the complete Lisp language without completely ignoring the way the computer system works, and performance remains an important indicator that we need to consider, so A large number of functional programming languages (functional programming), which have been silently growing in the corner of the computer world, are beginning to unfold.

Imperative programming, object-oriented programming, functional programming, although the popular time points are different, but in essence there is no good or bad points. Or they each have their own pros and cons, each with their own areas of suitability. For example, imperative language is more suitable for batch script writing, object-oriented language is more suitable for GUI interface processing, functional language is more suitable for parallel processing of large amount of data, and so on.

(Excerpt from "Object-oriented programming VS functional programming, address as follows: http://blog.swanspace.org/oo_vs_fp/")

    • Three main features of functional programming:
      • Immutable Data immutable : Like Clojure, the default variable is immutable, and if you want to change the variable, you need to copy the variable to modify it. This way, you can make your program a lot less bugs. Because, the state of the program is not good maintenance, in the concurrency is more difficult to maintain. (You can imagine if your program has a complex state, when others change your code, it is very easy to make a bug, in parallel in the problem is more)
      • First class functions: This technique allows your function to be used as a variable. In other words, your function can be created as a variable, modified, passed as a variable, returned, or nested within a function. This is a bit like JavaScript prototype (see JavaScript for Object-oriented programming)
      • tail recursion Optimization : We know the harm of recursion, that is, if recursion is deep, stack can not stand, and will cause a significant decline in performance. So, we use the tail recursive optimization technique, which reuses the stack every time it is recursive, which can improve performance, which, of course, requires language or compiler support. Python does not support it.
  • several techniques for functional programming
    • map & reduce  : This technique doesn't have to say much, the most common technique for functional programming is to map and reduce operations on a set. This is easier to read in code than a procedural language. (Traditional process language requires the use of for/while loops, and then inverted the data in various variables), which is much like the foreach,find_if,count_if of the STL in C + +.
    • Pipeline : This technique means that the function is instantiated as a single action, then a set of actions is placed in an array or list, and then the data is passed to the action list, The data is manipulated in sequence by functions like a pipeline, ultimately getting the results we want.
    • recursing recursive  : The greatest benefit of recursion is simplifying the code, and he can describe a complex problem in a very simple code. Note: The essence of recursion is to describe the problem, which is the essence of functional programming.
    • currying : Multiple parameters of a function are decomposed into multiple functions, and then the functions are encapsulated in layers, each function returns a function to receive the next parameter, which simplifies multiple parameters of the function. In C + +, this is much like the bind_1st or bind2nd in STL.
    • Higher order function Higher order functions : The so-called high-order function is the function when the argument, the incoming function to do a package, and then return this encapsulation function. The phenomenon is that functions pass in and out, just as object-oriented objects fly in and out.
  • And some of the benefits of a functional formula.
    • parallelization parallelism: The so-called parallelism means that in a parallel environment, there is no need for synchronization or mutual exclusion between threads.
    • Lazy Evaluation Lazy Evaluation : This requires compiler support. An expression is not evaluated immediately after it is bound to a variable, but is evaluated when the value is taken, that is, the statement, such as x:=expression; (Assigning the result of an expression to a variable) an explicit call to this expression is computed and the result is placed in x , but first, regardless of what is actually in x , until the x is passed through the following expression A reference to its value, and the evaluation of the subsequent expression itself can be deferred, eventually calculating the fast-growing dependency tree in order to generate a symbol to be seen by the outside world.
    • determinism certainty : the so-called certainty means that, as in mathematics, f (x) = y, this function will get the same result regardless of the scene, which we call the certainty of the function. Instead of the same parameters as many functions in a program, different results are computed in different scenarios. The so-called different scenarios mean that our functions change depending on the state information in the run.

(Excerpt from "Functional Programming", the address is as follows: Http://coolshell.cn/articles/10822.html, the original and a description of many examples)

PS: The original pickle λ calculus good useful, but do not understand ...

Sth about functional programming (functional programming)

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.