Functional Programming and C ++ template metaprogramming

Source: Internet
Author: User
Let's look at an example: # Include <stdio. h>
Template <int depth>
Class Fibnacci
{
Public:
Static const int value = maid <depth-1>: value + maid <depth-2>: value;
};

Template <>
Class Fibnacci <0>
{
Public:
Static const int value = 0;
};

Template <>
Class Fibnacci <1>
{
Public:
Static const int value = 1;
};

Template <int depth>
Void printFibnacci ()
{
PrintFibnacci <depth-1> ();
Wprintf (L "% d \ n", fig <depth >:: value );
}

Template <>
Void printFibnacci <0> ()
{
Wprintf (L "% d \ n", fig <0 >:: value );
}

Int main ()
{
Printfiber nacci <8> ();
Return 0;
}

I believe that a programmer can write the Fibnacci series. The point is that the computation of this Fibnacci series is completely completed during compilation! The same is true for the later print. When you tune the parameter, the running time will not be changed, but you will spend a long time in the compilation phase.
If you have heard of some template meta-programming, you will surely know that "C ++ templates are completely Turing. How is template meta completely Turing? The answer is that the template element and Functional have the same principle.
The essence of a template is definition and replacement. The essence of a lambda function is definition and replacement. The replacement here is actually in line with the lambda algorithm theory in mathematics:

Lambda Algorithm

Lambda Algorithm(Lambda calculusIs a form system used to study function definitions, function applications, and recursion. It was introduced by Alonzo Church and Stephen Cole Kleene, his student, in 1930s. Using the λ algorithm, the Church gave a negative answer to the criterion (Entscheidungsproblem) in 1936. This kind of calculation can be used to clearly define what a computable function is. The proposition about whether two lambda computing expressions are equivalent cannot be solved through a "general algorithm". This is the first problem that can be proved by the non-deterministic nature, and is even at the beginning of the downtime issue. Lambda calculus has a huge impact on functional programming languages, such as Lisp, ML, and Haskell.

Lambda calculus can be called the smallest universal programming language. It includes a conversion rule (variable replacement) and a function definition method. Lambda calculus is used to express and evaluate any computing function in this form. Therefore, it is equivalent to a Turing machine. Even so, Lambda calculus emphasizes the use of transformation rules rather than the specific machines that implement them. We can think of it as a way closer to software than hardware. V

Therefore, the C ++ template metaprogramming is actually a function-style programming, which is why many C ++ Programmers think it is uncomfortable.

In addition, the so-called Turing complete language can certainly be used to express any algorithm. Therefore, the direct fiber nacci we are using is a very inefficient algorithm.
There is a common saying: "The iterative algorithm of fiber nacci is faster than the recursive algorithm". Here I want to emphasize that recursion is only a form and has nothing to do with the algorithm. Here we provide O (n) (This Time, the compiler is not so tortured ): # Include <stdio. h>
Template <int depth>
Class Fibnacci
{
Public:
Static const int value = maid <depth-1>: value + maid <depth-1 >:: last;
Static const int last = maid <depth-1>: value;
};

Template <>
Class Fibnacci <0>
{
Public:
Static const int value = 0;
};

Template <>
Class Fibnacci <1>
{
Public:
Static const int value = 1;
Static const int last = 0;
};

Template <int depth>
Void printFibnacci ()
{
PrintFibnacci <depth-1> ();
Wprintf (L "% d \ n", fig <depth >:: value );
}

Template <>
Void printFibnacci <0> ()
{
Wprintf (L "% d \ n", fig <0 >:: value );
}

Int main ()
{
Printfiber nacci <8> ();
Return 0;
}

Last, leave a question. If you are interested, you can do it. Reply to the question below or leave a link. You can use the C ++ template or C # lambda function:
At a conference in 1994, Erwin Unruh wrote a program to print out a prime number sequence in the compilation error. At that time, the program was shocked by several masters present, including Bjarne Stroustrup, father of C ++. This incident marks the discovery of the Turing completeness of the C ++ template system. So, let's also pay tribute to our predecessors to print out a template with a prime number sequence!

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.