SoFunction
Updated on 2025-04-14

Non-nonsense JavaScript Tutorial (full episode) Page 3/4


V. Functional formula
This is not the invention of JavaScript. Its inventor is dead, and his invention is still bothering us... like Edison's lamp
The bubbles are still shining on us.
In fact, functional languages ​​are very simple. They are a language implementation solution that is as "complete" as imperative languages. Due to its basic ideas and commands
Formula - If you don't want to use this difficult noun, replace it with C, or Delphi - the language is completely different, so most
In the case, it is also out of place with these traditional, universal, commercial languages.
And in fact, you use it every day.
The following line of code is full of the ideas of functional languages:
a + b
Yeah? Really, if you think of that "+" sign as a function, it's exactly the same. In fact, the so-called function is a
Execution process, a piece of operation, a function entry...that is, something in the code is either data or it is an operation. And
If it is an operation, you can think of it as a "function". In the above line of code - in the expression, a and b are obviously data, while the + sign is the progression
operation, so it can naturally be regarded as a "function, process, or operation". So... the "function" that operates the sum of two numbers can be written
So this:
func_add(a, b)
Or this:
(+ a b)
All of this is just the difference in marking methods on the text. It's like I said that this symbol is "jia", and you have to say that it is "dark", another
But people have to pronounce it as "a-d-d". Is there any difference? No.
All programmers write statements, expressions, and operation logic every day. Everyone knows that these things can be written in one...well...unparalleled
Large functions, or divided into countless small, beautiful functions. When all these functions are successively calculated,
It becomes a "functional language". Therefore, LISP, called "the ancestor of functional languages", is a language that "connects function operations":
Copy the codeThe code is as follows:

(defun subst (x y z)
(cond ((atom z)
(cond ((eq z y) x)
('t z)))
('t (cons (subst x y (car z))
(subst x y (cdr z))))))

Some think it's ugly spaghetti, and some think it's the simplest language. Oh, just let it go, just understand it, no matter what kind of nun it is.
Since functional languages ​​only execute functions continuously - so they have no "statements" and must achieve logical integrity. Simply put, he
It is necessary to implement the three basic logics of "order, branch and loop" in the process of continuous execution. The solution to the functional formula is to use ternary conditional operations
Instead of if/then/else, the following code:
if a then b else c
// equivalent to
a ? b : c
In addition, it is to use recursion (function call function) to implement loops. Considering that recursive function calls will cause stack overflow (Stack overflow at ...),
Therefore, the functional language proposes "tail recursion", which means that when writing code is "ensure" to be called recursively only in the last operation in the function.
In this way, this recursive call does not need to keep the stack - because there is no subsequent operation, it can be optimized to a line that does not need to be returned.
jmp assembly instructions.
The world is so beautiful. The so-called functional formula is just a bunch of operation entrances, as well as jmp, jmp, and jmp. But, but, isn't it - this CPU?
6. More advanced functional formulas (1)
In fact, the world is not beautiful.
If a CPU is lying there, it will only have instructions such as order, branch, and loop. But when it runs, there must be a
The clock is ticking... If it is multi-core, there will be several such things at the same time.
This is the problem. The world is not a single time sequence, nor is it abides by order. So there will be concurrency, interrupts, and exceptions. Functional languages ​​should
You should expand as infinitely as spaghetti. So should you have multiple spaghetti when there are multiple drips? But,
Is it still called spaghetti?
The solution in the functional formula is called Continuation and nodes. Continuation is to stop the current and go back to another place; the node is true
Keep the code in one location independent and complete, and has nothing to do with another node. In functional languages, multiple nodes are like parallel universes, most of the emotions
In the case, they are transparent to each other, and if they are to connect, they need extremely huge energy, and... a wormhole.
We do not need to investigate the problem of multiple nodes. The space-time problems caused by access in multiple spaces are studied by celestialists and multidimensional theorists of space-time.
The problem we just need to know: if you want cross-access relationships between multiple nodes, the world/universe will be destroyed. Such a
Similar instructions are written in white papers, reference manuals, and daily guides for astronauts, such as ErLang, which are naturally supported by multiple universes. If you want to be poor
It is the root cause, and I believe that I have already clearly understood more than 300 advanced numbers, physics and celestial academic terms. There is a process under the Earth.
The entry guide for the sequel, please start from here:
Previous page1234Next pageRead the full text