Use C language to imitate Python functions and python Functions

Source: Internet
Author: User

Use C language to imitate Python functions and python Functions

First, you must note that C is not a functional programming language. To fully implement functional programming, you must first write a virtual machine and then write an interpreter (equivalent to CPython ).

The following example shows that C functions can "moderately imitate" Python functions.

We have the following Python program:

1 def line_conf(a, b):2     def line(x):3         return a*x + b4     return line5 6 line1 = line_conf(1, 1)7 line2 = line_conf(4, 5)8 print(line1(5), line2(5))
Python Code

 

In the C program, we simulate the line_conf function appropriately:

  1 /* MIT License  2   3 Copyright (c) 2017 Yuandong-Chen  4   5 Permission is hereby granted, free of charge, to any person obtaining a copy  6 of this software and associated documentation files (the "Software"), to deal  7 in the Software without restriction, including without limitation the rights  8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell  9 copies of the Software, and to permit persons to whom the Software is 10 furnished to do so, subject to the following conditions: 11  12 The above copyright notice and this permission notice shall be included in all 13 copies or substantial portions of the Software. 14  15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 SOFTWARE. */ 22  23 /////////////////////////////////////////////////////////////////////////////// 24  25 // Note: The C program is almost equivalent to the Python program as follows: 26 // def line_conf(a, b): 27 //     def line(x): 28 //         return a*x + b 29 //     return line 30 // 31 // line1 = line_conf(1, 1) 32 // line2 = line_conf(4, 5) 33 // print(line1(5), line2(5)) 34  35 #include <stdio.h>  36 #include <stdlib.h>  37 #include <unistd.h>  38 #include <stdarg.h> 39  40 typedef int Func(); 41  42 Func *line_conf(int x, int y,...) 43 {  44     va_list ap;  45     va_start(ap, y); 46  47     asm volatile( 48         "push %%eax\n\t" 49         "subl $40, %%esp\n\t" 50         "movl 8(%%ebp), %%eax\n\t" 51         "movl %%eax, -36(%%ebp)\n\t" 52         "movl 12(%%ebp), %%eax\n\t" 53         "movl %%eax, -40(%%ebp)\n\t" 54         "addl $40, %%esp\n\t" 55         "pop %%eax\n\t" 56         :::"memory" 57         ); 58  59 if(va_arg(ap,int) == 1){ 60  61 LINE: 62  63     asm volatile( 64         "push %%ebp\n\t" 65         "movl %%esp, %%ebp\n\t" 66         "movl 8(%%ebp), %%eax\n\t" 67         "imul -36(%%ebp), %%eax\n\t" 68         "addl -40(%%ebp), %%eax\n\t" 69         "movl %%ebp, %%esp\n\t" 70         "pop %%ebp\n\t" 71         "ret\n\t" 72         :::"memory","%eax" 73         ); 74 }    75 __END:   76     va_end(ap); 77     return (Func *)(&&LINE); 78 } 79  80 int main(int argc, const char *argv[]){   81     printf("====TEST START====\n"); 82     printf("34*234+6 ?= %d\n",line_conf(34,6)(234)); 83     printf("1*3+2 ?= %d; 324*65+3 ?= %d; 13*66+2 ?= %d\n",line_conf(1,2)(3),line_conf(324,3)(65),line_conf(13,2)(66)); 84  85     int fd = line_conf(1,6)(4); 86     Func *fun = line_conf(3,3); 87     int a = 1;  // Limited point 88     printf("3*3+3 ?= %d; 1*4+6 ?= %d\n",fun(3),fd); 89     printf("====TEST END====\n"); 90     return 0;   91 } 92  93 // Compile it by the following command: 94 // gcc -m32 -O0 -fno-stack-protector CFunctional.c; ./a.out 95 // The terminal output should looks like: 96 // ====TEST START==== 97 // 34*234+6 ?= 7962 98 // 1*3+2 ?= 5; 324*65+3 ?= 21063; 13*66+2 ?= 860 99 // 3*3+3 ?= 12; 1*4+6 ?= 10100 // ====TEST END====101 //Note: The limitation happens between line 86 and line 88, we cannot insert any function here102 // whose stack is larger than 40 bytes.(Why is 40? check the inline assembler language)
C Code

As a result, both MacOSX and Ubuntu (i386) can pass simple tests. However, we can see that it is only a simple simulation, and we also need to use a large number of (proportionally) assemblies, which have poor readability, a limited degree of simulation, and a longer code length. On the contrary, for such general functions, Python can easily simulate C-language functions with a high degree of simulation.

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.