View F # functional programming ideas from the perspective of the quality of C ++ template metaprogramming

Source: Internet
Author: User

Wen/Li Bo (Guang Yu guangzhen)

In other words, Erwin Unruh showed a piece of code that can generate a prime number during a meeting held by the C ++ Standards Committee in San Diego on March 13, 1994. The special feature of this Code is that the prime number is generated in the compilation period rather than the runtime period, and contains all the prime numbers between 2 and a set value in the error message generated by the compiler. Its release version is said to have been verified on GCC 3.2.

The source code will not be posted here. You can find it online or in any template meta programming (C ++ template meta-Programming) book. Here, I have rewritten this article to describe the ideas in this article more concisely:

Struct LB {lb (void *) {}; <br/> // The iteration process is replaced by recursive cyclic computing. <br/> template <int N, int I> struct isprime <br/> {Enum {Prime = (n = 2) | (n> 1) & (N % I) & isprime <n, i-1 >:: prime };< br/> // special, enumeration special circumstances: recursive end condition <br/> template <int n> struct isprime <n, 1 >{ Enum {Prime = 1 };}; <br/> // if n is a prime number, 1 is assigned to LB and an error is returned. If n is not a prime number, zero-value LB constructor <br/> template <int n> struct primeprint <br/> {<br/> Enum {Prime = isprime <n, n/2 >:: Prime }; <br/> primeprint <n-1> P; <br/> primeprint () {LB lB = Prime? 1: 0 ;}< br/>}; <br/> // all features, processing recursive end conditions <br/> template <> struct primeprint <1> {}; <br/> # define last 10 <br/> int _ tmain (INT argc, _ tchar * argv []) <br/>{< br/> primeprint <last> P; <br/> return 0; <br/>}

This code can no longer print all the prime numbers on vs 2010. Instead, it only prints the prime number closest to the last one. Here is 7. At this point, the deep recursion is stopped, this is not counted.Intelligent Compiler...... If any reader has other compilers, they can perform self-verification.

Intuitively, the process of verifying the prime number is iterative division. If we find that the number N is a multiple of M (1 <m <= n/2), n is not a prime number. The iteration process can be completed by both the for loop and the function recursion. Obviously, the template metadata cannot be cyclically determined by running actions such as for, while, And if. What is the unique operator that can be used? : Expression. Therefore, the iteration process can only be done using recursion. Recursive termination conditions are described using a special template.

This actually reflects the idea of functional programming-replacing for/wihle and if/else with recursive and pattern matching.At this point, the two ideas are common.

In other words,Loop and branchIs the focus of learning programming. Beginners have no idea about these two parts. In fact, loops and branches provide efficient program flow control solutions, which implies great security and performance risks. For/while loops are subject to incremental control and more complex boundary checks. If/else loop flooding is a disaster for Intel series Processors. In addition to C/ASM, a slightly more advanced language is working on these two blocks. For example, C ++/C # allows foreach to replace for to avoid the annoyance of incremental control and boundary check. It advocates the idea of OOP to use design patterns to eliminate if/else blocks as much as possible. However, it does not cure the problem.

WhileF # as an excellent functional programming languageWhile retaining for and if to adapt to the programmer's old habits, it focuses onRecursion and pattern matchingProgram Design Ideas to solve these two key issues.Compared with the disadvantages of the OOP solution selected by C ++/C #, the code written using the design concept of F # is elegant and rigorous, and shows the excellence of functional programming.:

Let isprime n = <br/> let rec examprime n I = <br/> match N, I with <br/> | 2, i-> true // bitwise <br/> | N, 1-> true // bitwise <br/> | n, I when n <2 | n % I = 0-> false <br/> | N, I-> examprime N (I-1) <br/> examprime n (n/2)

In C ++ template metaprogrammingTemplate SpecializationIt is a"Pattern Matching"The concept is loose, and in F #, this kind of excellent design idea is presented in a centralized manner, as shown in the matching of the two lines mentioned in the code, this is equivalent to bitwise processing of C ++ templates. Similar good ideas are embodied in F # elegantly and tightly. This is the charm of functional programming.

Another article on F # functional language ideology embodied in template metaprogramming:View F # Processing of linked lists through C ++ template meta-Programming.

F # more:In my eyes, F #'s prospects and C #'s failure,Lop in F # -- "language-oriented programming",Microsoft's big strategy in my eyes and the idea of Chinese Software.

Related Article

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.