One day for four programmers

Source: Internet
Author: User
Tags dot net dotnet
[Statement: This article does not devalue a certain Programming Language Meaning]

Please note: 2005/10/14
I found some friends republished this post and forget keep the original information, so please:

1. Please don't forget keep the original address in your post.
2. Please don't modify it.

Thanks.

You, a DOTNETProgramMember, just joined a new project team. Except you, other members include: ceer, a programmer who has been engaged in C projects. He just transferred to C # for less than a month; jally, holding the design pattern all day (yes, it's the gof's) The former Java programmer, and semon, you have no idea about him, I just heard from PM that he was engaged in scheme (one of the legends of the second Ancient Language LISP ). But you don't care about it either. After all, computers are not sweet.

On Monday, when I started my computer, my boss ran to the office seat of your group and said, "Okay, guys, you need a function now. Specifically, you can enter two numbers and an operator. You can calculate the result based on the input. "

Example: Foo (+, 1, 2) = 3; Foo (*, 3, 6) = 18; Foo (/, 2, 4) = 0.5

Ceer is the first to respond: It's easy to judge the input operator. He quickly wrote the following on the whiteboard: Code :
Public class cstyle_calculator
{
Static public double Foo (char op, double X, Double Y)
{
Switch (OP)
Case '+': Return X + Y; break;
Case '-': Return x-y; break;
Case '*': Return x * Y; break;
Case '/': Return x/y; break;
Default: Throw new exception ("what the hell you have input? ");
}
}

Jally only read it once and shook his head with his nose covered: a bad smell of code [NOTE 1 ]. It is better to see that I use the OO method to solve the problem:
Public interface I operator // who says the Code cannot write Chinese? Nun
{
Double (Double X, Double Y );
}

Public class oo_calculator
{
Private I operator m_op;
Public oo_calculator (I operator OP)
{
This. m_op = op; // dependency injection [NOTE 2]
}

Public double Foo (Double X, Double Y)
{
Return this. m_op. Operation (x, y );
}
}

Public class addition: I Operator
{
Public double (Double X, Double Y)
{
Return X + Y;
}
}

Public class subtraction: I Operator
{
Public double (Double X, Double Y)
{
Return x-y;
}
}

Public class multiplication: I Operator
{
Public double (Double X, Double Y)
{
Return x * Y;
}
}

Public class division: I Operator
{
Public double (Double X, Double Y)
{
Return x/y;
}
}

Public class themainclass
{
Static public void main ()
{
I operator my addition = new addition ();
Oo_calculator my addator = new oo_calculator (my addition );
Double sum = My multiplier. Foo (3, 4 );
System. Console. writeline (SUM );
// Sum = 7

// I will not talk nonsense about the other three.
}
}

You looked at jally and wrote the whiteboard in a dense manner. Then, you shrugged and sighed. You used Java waste, so it was annoying to use the interface tool. Let me show you how powerful DOTNET is. Isn't it good for me to directly use the delegate operator to transfer it in.
Public Delegate double theoperator (Double X, Double Y );

Public class Operators
{
Static public double add (Double X, Double Y)
{
Return X + Y;
}

Static public double sub (Double X, Double Y)
{
Return x-y;
}

// Multiplication. I am too tired to talk nonsense about Division.
}

Public class dotnet_calculator
{
Public double Foo (theoperator op, double X, Double Y)
{
Return OP (x, y );
}
}

Public class themainclass
{
Static public void main ()
{
Theoperator myadd = new theoperator (operators. Add );
Theoperator mysub = new theoperator (operators. sub );

Dotnet_calculator Dc = new dotnet_calculator ();
Double sum = Dc. Foo (myadd, 2, 4); // sum = 6
System. Console. writeline (SUM );
Double sub = Dc. Foo (mysub, 3, 7); // sub =-4
System. Console. writeline (sub );
}
}
// Under dot net, you can also use codedom to dynamically construct C # code and compile and run it in the memory.
// If it is annoying to write operators specifically, try the anonymous method C #2.0.

Well, after you finish writing the code and look at jally, ceer starts to complain: "Isn't that the function pointer in C? I will..."
"But the delegate under DOTNET is a type of security drops..." you continue to be proud.

Semon looked at your three gorgeous codes and didn't say anything. I just typed two lines of code on the keyboard.

(Define (FOO op x y)
(Op x y ))

Then I got off work...

[Note: The scheme code is explained below: (+ 1 2) = 3, (* 3 4) = 12 .]
As for the semon solution:
(Define (FOO op x y)
(Op x y ))

The above code has only one function: the first line is the function header, which defines a function named Foo. This function accepts three parameters op, X, and Y.
The second row defines the function behavior: the first parameter op is used as an operator to calculate the next two parameters.
So :( Foo + 1 2) = 3. (FOO/12 6) = 2.

Okay, no more stories.
I just want to make everyone look at the wonderful world of function programming after busy work. Function programming, the biggest feature is that it treats functions as 1st class elements in the language. A function can take another function as a parameter, or return a function as a result. Such a function is called the higher-order function.

So what are the differences between function programming and our traditional object-oriented systems? Well, that's the difference between the Turing Machine and the Von northeman system. However, a sentence can better summarize the difference between FP and OO (well, this is also the one on the "zipi book ):

"Pascal is to build the pyramid... LISP is used to build organisms... "" As the internal data structure of lisp, tables play an important role in improving this availability... "using 100 functions to operate on a single data structure is far better than using 10 operations to work on ten data structures." "The pyramid stands there for thousands of years, while the organism must evolve, otherwise it will die ".

The other well summarized statement is: (copied as well)

An object: A group of identical operations plus different data. (Think about your object, isn't it like this ?)
A closure: A group of identical data plus different operations. (Delegate is such an idea. If you are interested, you can check Ruby)

Basically, well, nothing to say. If you are interested, you can take a look at the MIT SiC course (with an online version, MIT is also set up as an Open Course course)

References:
Function programming in Java (even the entry to FP. Uncle Cha, I worship you)

Http://www.hibernate.org.cn/viewtopic.php? T = 7569 & postdays = 0 & postorder = ASC & START = 0

Lambda calculus

Http://www.mactech.com/articles/mactech/Vol.07/07.05/LambdaCalculus/

Function programming in Java

Http://www-128.ibm.com/developerworks/cn/java/j-fp/

[NOTE 1]
See uncle Bob's ASD.

[NOTE 2]
The dependency injection mode of flower. What is the IOC container here?

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.