Recently, I have been preparing for the final semester, and then I am preparing for the spring recruitment of the 19th internship at Panic. I feel so panic that I can’t find an internship.

However, I found that the previous translation of the article, this is a series of articles, is also my functional programming into the pit for it

I just translated the first one, the second one and the third one, which are all in progress. If you are interested, I will release them one by one

No, then pit…

When I was six years old, I liked playing computer games with my friends. His house has a special room full of computers. To me, their home was like a magical land that attracted me. After spending a lot of time playing a lot of games, I asked my friends, “How do we make one? “

He didn’t know either, so we asked his dad, who pulled a book on Basic off the shelf. So I started my programming journey. I learned algebra when I started teaching it in school, because programming is algebra.

The rise of Composing Software

In the early days of computer science, before a lot of computer science was practiced on computers, there were two great computer scientists: Alonzo Church and Alan Turing. They developed two different but widely used computational models. Either model can evaluate any computable model.

Alonzo Church invented the lambda calculus. The lambda calculus is a general model based on the application of functions. Alan Turing is best known for the Turing machine, a universal model that defines devices and operators in a roll of tape.

The collaboration between these two models shows that the two models actually lead to the same destination in terms of function.

The calculus of λ is mainly concerned with combinations of functions. It is very intuitive to think of a problem in terms of function composition, and it is very effective for combinators. Next, we will discuss the importance of function composition in programming.

The lambda calculus is special for three reasons:

  1. Functions are usually anonymous. In JavaScript,const sum = (x, y) => x + yThe right-hand operand of is an anonymous function expression.
  2. In the calculus of λ the function takes only a single input and is unary. If you need more arguments, the function will take an input and return a new function to accept the new arguments. Multivariate function(x, y) => x + yCan be expressed asx => y => x + yA function of one variable. Such a change is called currying of programming
  3. Functions are first-level, which means functions can be used as output of other functions, and functions can return functions.

These features give combinatorial programs the ability to use functions as basic building blocks. Anonymous functions and currie functions are both available features in JavaScript. Given JavaScript’s features of the λ calculus, it is appropriate to use the λ calculus model.

The classical combination of functions extracts the input from the output of one function into another, for example:

f . g
Copy the code

It can be written like this:

compose2 = f => g => x => f(g(x))
Copy the code

It can be used like this:

double = n => n * 2
inc = n => n + 1
Copy the code
compose2(double)(inc)(3)
Copy the code

Compose2 () takes double as the first argument, inc as the second, and finally 3 for the combined function. Looking at the signature of compose2(), f is double(), g is inc(), and x is 3. The final call to compose2(double)(inc)(3) is actually three different function calls:

  1. First to receivedoubleA new function is returned.
  2. And then the function acceptsincReturns a new function.
  3. The new function is accepted3And then execute itf(g(x))It is nowdouble(inc(3)).
  4. xIs equal to the3After the incominginc().
  5. inc(3)Is equal to the4.
  6. double(4)Is equal to the8.
  7. 8It’s returned by the function.

When the program is composed, it can graphically represent the function composition process. Here’s an example:

append = s1 => s2 => s1 + s2
append('Hello, ') ('world! ')
Copy the code

Can be expressed like this:

The lambda calculus had a great influence on programming until 1980, when there were many computer science results based on combinatorial functions. Lisp was born in 1958 and was greatly influenced by the lambda calculus. Lisp is still the second oldest language spoken today.

I learned about Lisp through AutoLISP. AutoLISP is a language for CAD. AutoCAD’s popularity certainly supports AutoLISP, but basically all major CAD software supports AutoLISP. Lisp is still a popular language today for three reasons:

  1. It takes about a day to learn Lisp symbols and grammar.
  2. Lisp is a combination of functions, an elegant way to structure programming.
  3. I know that the best CS textbook, Structure and Interpretation of Computer Programs, uses Lisp

The decline of Composing Software

In the 1970s, the model of creating programs moved away from simple combination checks to industrial assembly-line production. Then came object-oriented programming, a good idea about component encapsulation and information passing. But it’s a nightmare of inheritance and class relationships, thanks to some popular language distortions.

Functions are the reason programming has lost its place both academically and in applications, the preserve of obsessive geeks, ivy League professors, and students who haven’t fallen in love with the Java fad.

It was a dark time for most people.

Composing Software is on the rise again

Then, around 2010, a good thing happened: the JavaScript explosion. Before 2006. JavaScript is thought of as a toy language for web animation, but it actually has a lot of powerful features hidden in it, namely the lambda calculus. People are beginning to talk about a “new concept” : “functional programming”.

In 2015, the concept of composed programs began to rise again. To make it more common, JavaScript has also been updated with new features, including arrow functions that make functions, currization, and λ expressions more intuitive and readable.

Arrow functions are functionally explosive rocket fuel, so much so that it’s hard to see a JavaScript program that doesn’t use much functional programming these days.

Composition makes software models and behaviors simpler, more elegant, and more intuitive. Using lightweight but important functions to build larger components, functional writing is easier to test, understand, organize, and introduce.

英 文 : The Rise and Fall and Rise of Functional Programming (Composing Software)

By Eric Elliott

Translation: Dominic Ming