用C语言模仿Python函数的实例
首先得说明一点,C语言不是函数式编程语言,要想进行完全的函数式编程,还得先写个虚拟机,然后再写个解释器才行(相当于CPython)。
下面我们提供一个例子,说明C语言函数可以“适度地模仿”Python函数。
我们有如下的Python程序:
defline_conf(a,b): defline(x): returna*x+b returnline line1=line_conf(1,1) line2=line_conf(4,5) print(line1(5),line2(5))
我们在C程序中适度地模拟其中的line_conf函数:
/*MITLicense Copyright(c)2017Yuandong-Chen Permissionisherebygranted,freeofcharge,toanypersonobtainingacopy ofthissoftwareandassociateddocumentationfiles(the"Software"),todeal intheSoftwarewithoutrestriction,includingwithoutlimitationtherights touse,copy,modify,merge,publish,distribute,sublicense,and/orsell copiesoftheSoftware,andtopermitpersonstowhomtheSoftwareis furnishedtodoso,subjecttothefollowingconditions: Theabovecopyrightnoticeandthispermissionnoticeshallbeincludedinall copiesorsubstantialportionsoftheSoftware. THESOFTWAREISPROVIDED"ASIS",WITHOUTWARRANTYOFANYKIND,EXPRESSOR IMPLIED,INCLUDINGBUTNOTLIMITEDTOTHEWARRANTIESOFMERCHANTABILITY, FITNESSFORAPARTICULARPURPOSEANDNONINFRINGEMENT.INNOEVENTSHALLTHE AUTHORSORCOPYRIGHTHOLDERSBELIABLEFORANYCLAIM,DAMAGESOROTHER LIABILITY,WHETHERINANACTIONOFCONTRACT,TORTOROTHERWISE,ARISINGFROM, OUTOFORINCONNECTIONWITHTHESOFTWAREORTHEUSEOROTHERDEALINGSINTHE SOFTWARE.*/ /////////////////////////////////////////////////////////////////////////////// //Note:TheCprogramisalmostequivalenttothePythonprogramasfollows: //defline_conf(a,b): //defline(x): //returna*x+b //returnline // //line1=line_conf(1,1) //line2=line_conf(4,5) //print(line1(5),line2(5)) #include#include #include #include typedefintFunc(); Func*line_conf(intx,inty,...) { va_listap; va_start(ap,y); asmvolatile( "push%%eax\n\t" "subl$40,%%esp\n\t" "movl8(%%ebp),%%eax\n\t" "movl%%eax,-36(%%ebp)\n\t" "movl12(%%ebp),%%eax\n\t" "movl%%eax,-40(%%ebp)\n\t" "addl$40,%%esp\n\t" "pop%%eax\n\t" :::"memory" ); if(va_arg(ap,int)==1){ LINE: asmvolatile( "push%%ebp\n\t" "movl%%esp,%%ebp\n\t" "movl8(%%ebp),%%eax\n\t" "imul-36(%%ebp),%%eax\n\t" "addl-40(%%ebp),%%eax\n\t" "movl%%ebp,%%esp\n\t" "pop%%ebp\n\t" "ret\n\t" :::"memory","%eax" ); } __END: va_end(ap); return(Func*)(&&LINE); } intmain(intargc,constchar*argv[]){ printf("====TESTSTART====\n"); printf("34*234+6?=%d\n",line_conf(34,6)(234)); 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)); intfd=line_conf(1,6)(4); Func*fun=line_conf(3,3); inta=1;//Limitedpoint printf("3*3+3?=%d;1*4+6?=%d\n",fun(3),fd); printf("====TESTEND====\n"); return0; } //Compileitbythefollowingcommand: //gcc-m32-O0-fno-stack-protectorCFunctional.c;./a.out //Theterminaloutputshouldlookslike: //====TESTSTART==== //34*234+6?=7962 //1*3+2?=5;324*65+3?=21063;13*66+2?=860 //3*3+3?=12;1*4+6?=10 //====TESTEND==== //Note:Thelimitationhappensbetweenline86andline88,wecannotinsertanyfunctionhere //whosestackislargerthan40bytes.(Whyis40?checktheinlineassemblerlanguage)
结果在MacOSX和Ubuntu上(i386)都能通过简单的测试。但是可以看到,仅仅是简单的模拟,我们也得用到大量(按比例)的汇编,可读性很差,而且模拟程度非常有限,代码长度也更长。相反,对于这类一般功能的函数,Python可以很容易地模拟C语言的函数,而且模拟程度很高。
以上这篇用C语言模仿Python函数的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。