Tag: 函数式编程

C中的函数式编程(Currying)/类型问题

作为一个染成羊毛的function性程序员,我发现很难不把我最喜欢的范例变成我正在使用的语言。 在编写一些CI时,我想要讨论我的一个函数,然后传递部分应用的函数。 看完之后有没有办法在C里做cur? 并注意到http://gcc.gnu.org/onlinedocs/gcc/Nested-Functions.html#Nested-Functions上的警告我提出: #include typedef int (*function) (int); function g (int a) { int f (int b) { return a+b; } return f; } int f1(function f){ return f(1);} int main () { printf (“(g(2))(1)=%d\n”,f1(g(2))); } 哪个按预期运行。 但是,我的原始程序使用double s,所以我想我只是改变了适当的类型,我会没事的: #include typedef double (*function) (double); function g (double a) { double f (double b) { […]

在C中,作为参数传递时,`&function`和`function`有什么区别?

例如: #include typedef void (* proto_1)(); typedef void proto_2(); void my_function(int j){ printf(“hello from function. I got %d.\n”,j); } void call_arg_1(proto_1 arg){ arg(5); } void call_arg_2(proto_2 arg){ arg(5); } void main(){ call_arg_1(&my_function); call_arg_1(my_function); call_arg_2(&my_function); call_arg_2(my_function); } 运行这个我得到以下内容: > tcc -run try.c hello from function. I got 5. hello from function. I got 5. hello from […]

C中的高阶函数

是否有一种“适当的”方式在C中实现更高阶的函数。 我对这里的可移植性和语法正确性等问题非常好奇,如果有多种方法,那么优点和缺点是什么。 编辑:我想知道如何创建更高阶函数的原因是我编写了一个系统来将PyObject列表(在调用python脚本时得到)转换为包含相同数据但以不同方式组织的C结构列表依赖于python.h库。 所以我的计划是有一个函数,它迭代pythonic列表并在列表中的每个项目上调用一个函数,并将结果放在一个列表中然后返回。 所以这基本上是我的计划: typedef gpointer (converter_func_type)(PyObject *) gpointer converter_function(PyObject *obj) { // do som stuff and return a struct cast into a gpointer (which is a void *) } GList *pylist_to_clist(PyObject *obj, converter_func_type f) { GList *some_glist; for each item in obj { some_glist = g_list_append(some_glist, f(item)); } return some_glist; } void some_function_that_executes_a_python_script(void) […]