What tools are there for functional programming in C?

后端 未结 13 832
长情又很酷
长情又很酷 2021-01-29 17:18

I\'ve been thinking a lot lately about how to go about doing functional programming in C (not C++). Obviously, C is a procedural language and doesn\'t really support f

13条回答
  •  滥情空心
    2021-01-29 18:00

    You can use GCC's nested functions to simulate lambda expressions, in fact, I have a macro to do it for me:

    #define lambda(return_type, function_body) \
      ({ \
        return_type anon_func_name_ function_body \
        anon_func_name_; \
      })
    

    Use like this:

    int (*max)(int, int) = lambda (int, (int x, int y) { return x > y ? x : y; });
    

提交回复
热议问题