首先得说明一点,C 语言不是函数式编程语言,要想进行完全的函数式编程,还得先写个虚拟机,然后再写个解释器才行(相当于 CPython )。

下面我们提供一个例子,说明即便不写虚拟机, C 语言函数也是可以“适度地模仿” Python 函数。

我们有如下的 Python 程序:

 def line_conf(a, b):
def line(x):
return a*x + b
return line line1 = line_conf(1, 1)
line2 = line_conf(4, 5)
print(line1(5), line2(5))

Python Code

我们在C程序中适度地模拟其中的line_conf函数:

 /* MIT License

 Copyright (c) 2017 Yuandong-Chen

 Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. */ /////////////////////////////////////////////////////////////////////////////// // Note: The C program is almost equivalent to the Python program as follows:
// def line_conf(a, b):
// def line(x):
// return a*x + b
// return line
//
// line1 = line_conf(1, 1)
// line2 = line_conf(4, 5)
// print(line1(5), line2(5)) #include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdarg.h> typedef int Func(); Func *line_conf(int x, int y,...)
{
va_list ap;
va_start(ap, y); asm volatile(
"push %%eax\n\t"
"subl $40, %%esp\n\t"
"movl 8(%%ebp), %%eax\n\t"
"movl %%eax, -36(%%ebp)\n\t"
"movl 12(%%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) == ){ LINE: asm volatile(
"push %%ebp\n\t"
"movl %%esp, %%ebp\n\t"
"movl 8(%%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);
} int main(int argc, const char *argv[]){
printf("====TEST START====\n");
printf("34*234+6 ?= %d\n",line_conf(,)());
printf("1*3+2 ?= %d; 324*65+3 ?= %d; 13*66+2 ?= %d\n",line_conf(,)(),line_conf(,)(),line_conf(,)()); int fd = line_conf(,)();
Func *fun = line_conf(,);
int a = ; // Limited point
printf("3*3+3 ?= %d; 1*4+6 ?= %d\n",fun(),fd);
printf("====TEST END====\n");
return ;
} // Compile it by the following command:
// gcc -m32 -O0 -fno-stack-protector CFunctional.c; ./a.out
// The terminal output should looks like:
// ====TEST START====
// 34*234+6 ?= 7962
// 1*3+2 ?= 5; 324*65+3 ?= 21063; 13*66+2 ?= 860
// 3*3+3 ?= 12; 1*4+6 ?= 10
// ====TEST END====
//Note: The limitation happens between line 86 and line 88, we cannot insert any function here
// whose stack is larger than 40 bytes.(Why is 40? check the inline assembler language)

C Code

结果在MacOSX和Ubuntu上(i386)都能通过简单的测试。但是可以看到,仅仅是简单的模拟,我们也得用到大量(按比例)的汇编,可读性很差,而且模拟程度非常有限,代码长度也更长。

注意:这只是个玩具,别这么写代码,除非想搞破坏(充斥着各种漏洞,极易被侵入)。

用C语言模仿Python函数的更多相关文章

  1. 在使用python语言的open函数时,提示错误OSError: [Errno 22] Invalid argument: ‘文件路径’

    如题,在使用python语言的open函数时,提示错误OSError: [Errno 22] Invalid argument: '文件路径',在查阅了大量资料后也得到了一些解决方案,但是这些解决方案 ...

  2. Python函数参数默认值的陷阱和原理深究"

    本文将介绍使用mutable对象作为Python函数参数默认值潜在的危害,以及其实现原理和设计目的 本博客已经迁移至: http://cenalulu.github.io/ 本篇博文已经迁移,阅读全文 ...

  3. C语言扩展Python模块

    1. 先创建一个PythonDemo.cpp文件: //c/c++中调用python脚本,配置步骤参见上一篇:C/C++与python交互 \  C/C++中调用python文件. #include ...

  4. Python开发【第三章】:Python函数介绍

    一. 函数介绍 1.函数是什么? 在学习函数之前,一直遵循面向过程编程,即根据业务逻辑从上到下实现功能,其往往用一长段代码来实现指定功能,开发过程中最常见的操作就是粘贴复制,也就是将之前实现的代码块复 ...

  5. Python函数解析

    对于Python的函数,我们需要记住的是: 1. 函数的默认返回值是None. 2. python是一个自上而下逐行解释并执行的语言.因此,函数的定义必须在函数被调用之前.同名的函数,后定义的会覆盖前 ...

  6. Python入门笔记(18):Python函数(1):基础部分

    一.什么是函数.方法.过程 推荐阅读:http://www.cnblogs.com/snandy/archive/2011/08/29/2153871.html 一般程序设计语言包含两种基本的抽象:过 ...

  7. python函数的返回值 讲解

    我们一起来聊聊python函数返回值的特殊情况,之前我也碰到过类似方面的问题,到后来查阅了一些资料后,发现原来是这样. 首先,写函数的时候,一定要写函数的文档,这样方便我们识别函数是做什么的.我记得很 ...

  8. windows 下 使用codeblocks 实现C语言对python的扩展

    本人比较懒就粘一下别人的配置方案了 从这开始到代码 摘自http://blog.csdn.net/yueguanghaidao/article/details/11538433 一直对Python扩展 ...

  9. python函数的参数传递问题---传值还是传引用?

    摘要:在python中,strings, tuples, 和numbers是不可更改的对象,而list,dict等则是可以修改的对象.不可更改对象的传递属于传值,可更改对象属于传引用.想要在函数中传递 ...

随机推荐

  1. 老李分享:Eclipse中开发性能测试loadrunner脚本

    老李分享:Eclipse中开发性能测试loadrunner脚本 前篇我分享了如何用loadrunner搭建javauser的性能测试脚本环境,本次我来告诉大家如何在eclipse开发loadrunne ...

  2. 老李分享:《Linux Shell脚本攻略》 要点(四)

    老李分享:<Linux Shell脚本攻略> 要点(四)   1.IP地址的正则表达式: [0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3} 2. ...

  3. span表情输入框 --- Author: rose && lvyerose@163.com

        像QQ等社交聊天中,不可或缺的一部分就是我们常用的表情输入了,有时候有趣的表情同样能吸引住用户达到用户常驻的效果,当然,我们开发的时候不用去研究如何才能做到有趣,如何才能做到足够吸引用户,我们 ...

  4. VS 2017开发ASP.NET Core Web应用过程中发现的一个重大Bug

    今天试着用VS 2017去开发一个.net core项目,想着看看.net core的开发和MVC5开发有什么区别,然后从中发现了一个VS2017的Bug. 首先,我们新建项目,ASP.NET Cor ...

  5. Netd学习笔记

    service netd /system/bin/netd     class main     socket netd stream 0660 root system     socket dnsp ...

  6. Struts2基础学习(二)—Action

    一.ActionSupport      为了让用户开发的Action类更加规范,Struts2提供了一个Action接口,这个接口定义了Struts2的Action处理类应该实现的规范.下面是标准A ...

  7. [Paxos] Paxos Made Simple 读后感

    Paxos 由著名图灵奖获得者Leslie Lamport提出,该算法是分布式一致性算法中的奠基之作,今天初读此文仅将相关学习心得予以记录. 1.Paxos 是什么?主要用来解决什么问题? Paxos ...

  8. 解决Tomcat: Can't load IA 32-bit .dll on a AMD 64-bit platform 问题

    错误如下: java.lang.UnsatisfiedLinkError: E:\Program Files\MyEclipse 10\apache-tomcat-7.0.23\bin\tcnativ ...

  9. 【stm32中断优先级--珍藏版】

    看了这么久,一直不理解中断优先级,还有中断嵌套.stm32提供了多种嵌套方式,搞的我真是头昏脑涨. 今天终于看到了一个通俗解释中断优先级的博客.算是理解了一点吧. 原文地址:http://blog.s ...

  10. git提交如何忽略某些文件

    在使用git对项目进行版本管理的时候,我们总有一些不需要提交到版本库里的文件和文件夹,这个时候我们就需要让git自动忽略掉一下文件. 使用.gitignore忽略文件 为了让git忽略指定的文件和文件 ...