The complexity of functional programming (very messy)

Source: Internet
Author: User
Tags dota
Document directory
  • 1. Put the elephant in the refrigerator.
  • 2.
  • 3. The language features and programming paradigm are totally different.
  • 4. Change the color of 10 buttons.
  • 5. paradigm is the methodology for understanding the world
1. Put the elephant in the refrigerator.

Process-oriented:

Put the elephant in the refrigerator:
Open the refrigerator door =>
Put the elephant in the refrigerator =>
Refrigerator door closed

Object-oriented:

Refrigerator. Open Door ()
Refrigerator. Put (ELEPHANT)
Refrigerator. Close ()

Function Type:

(Refrigerator, elephant ):
Close the door (put in (open the door (refrigerator), elephant ))
--------------------------

2.

C # the feature of introducing FP is called syntactic sugar by some people. I 've been trying to drag this idea out and beat it up (not to drag people who hold this idea out ), as for those who hold such ideas, I will prohibit you from writing a + B * c, and then write all the compilation for me.
Mov eax, [B]
Mul eax, [c]
Add eax, [a]

The premise is that I can disable it.

--------------------------

3. The language features and programming paradigm are totally different.

To some extent, C language can be both functional programming and

In an object-oriented language, I can design a class similar to the following.

Class Search {
Virtual List Extend (Node node)
Virtual bool Beam (Node node)
Virtual bool CheckFinish (Node node)
Node Search ()
{
OrderedCollection openlist = new OrderedCollection ();
Openlist. put (node );
While (! Openlist. empty ()){
Node current = openlist. get ();
If (! Beam (current )){
List extended = Extend (current );
For (var I = 0; I <extended. length; I ++ ){
If (Finish (extended [I])
Return extended [I];
Openlist. put (extended [I]);
}
}
}
Return null;
}
}

I even used a design pattern: Template Method.

However, this code has nothing to do with object-oriented code. I just use some language features.

For any programming language, it is the idea of programmers to regard class as a "class". The compiler doesn't care whether this keyword is class or glass, but it is just a keyword.

When speaking these languages, fathers consciously remind you that this feature will be used for Object-Oriented purposes, but they have not forced you to do so.

So some restless people (such as me) like to use object-oriented features to do things that are not object-oriented, or use features that are not object-oriented to do object-oriented things.

So for C, closure is nothing more than a void * than the function pointer, and passing one parameter is not a piece of meat-You dare say this is not a syntactic sugar?

However, using this method to implement FP is truly unknown. This shows that language features have a great impact on people's thinking.

So try not to do things that twist the language features, for example, express normal logic with something someone calls Exception.

--------------------------

4. Change the color of 10 buttons.

For (var I = 0; I <10; I ++)
Button [I]. background = XXXX;

This simple code loses an important piece of information, that is, "it is not required to change the color order for 10 buttons ".

Therefore, it is impossible to translate the code back into the original semantics.

But the function type is acceptable.

No function cares about the order in which its parameters are evaluated.

However, no function is executed before its own parameter is evaluated.

So put (open the door (refrigerator), the elephant will certainly be executed after the door (refrigerator.

These discarded information is particularly important in multi-core processors.

You write

DotA ();
DotB ();

I certainly don't know whether the doA and doB can be executed in parallel.

However

Do (dotA (), dotB ())

At this time, I certainly know that doA and doB can be done at the same time.

Forgive me for using dota for SEO ......

--------------------------

5. paradigm is the methodology for understanding the world

Keep working for 12 hours (as a programmer) and as a computer ),

Some bosses understand how to complete a project;

Whip programmer => ordering programmer to work on a computer => waiting for completion

Some bosses understand this as well.

When the boss thinks about "how to do the job", they may come to the same conclusion using different methods.

This has nothing to do with programming. When you start to think about the world, you will definitely choose one to understand the world.

You have been using the function.

In the world, no matter what you want to do, it is nothing more than turning something into something else. This is the world of functional programming.

It's just that you are not used to applying this kind of thinking to programs.

You always want to tell the computer, first, then, and then.

So if you really want to understand the functional formula, the key is the change of concept-you must completely discard the previous ideas.

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.