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
The way I went about doing functional programming in C was to write a functional language interpreter in C. I named it Fexl, which is short for "Function EXpression Language."
The interpreter is very small, compiling down to 68K on my system with -O3 enabled. It's not a toy either - I'm using it for all the new production code I write for my business (web-based accounting for investment partnerships.)
Now I write C code only to (1) add a built-in function that calls a system routine (e.g. fork, exec, setrlimit, etc.), or (2) optimize a function that could otherwise be written in Fexl (e.g. search for a substring).
The module mechanism is based on the concept of a "context". A context is a function (written in Fexl) which maps a symbol to its definition. When you read a Fexl file, you can resolve it with any context you like. This allows you to create custom environments, or run code in a restricted "sandbox."
http://fexl.com
The main thing that comes to mind is the use of code generators. Would you be willing to program in a different language that provided the functional programming and then generate the C code from that?
If that's not an attractive option, then you could abuse CPP to get part of the way there. The macro system should let you emulate some functional programming ideas. I've heard tell that gcc is implemented this way but I've never checked.
C can of course pass functions around using function pointers, the main problems are lack of closures and the type system tends to get in the way. You could explore more powerful macro systems than CPP such as M4. I guess ultimately, what I'm suggesting is that true C isn't up to the task without great effort but you could extend C to make it be up to the task. That extension would look the most like C if you use CPP or you could go to the other end of the spectrum and generate C code from some other language.
If you want to implement closures, you'll have to get groady with assembly language and stack swapping/management. Not recommending against it, just saying that's what you'll have to do.
Not sure how you'll handle anonymous functions in C. On a von Neumann machine, you could do anonymous functions in asm, though.
Look at Hartel & Muller's book, Functional C
http://www.ub.utwente.nl/webdocs/ctit/1/00000084.pdf
http://www.cs.bris.ac.uk/~henkm/f2c/index.html
Dont Know about C. There are some functional features in Objective-C though, GCC on the OSX also supports some features, however I would again recommend to start using a functional language, there are plenty mentioned above. I personally started off with scheme, there are some excellent books such as The Little Schemer that can help you do so.
Well quite a few programming languages are written in C. And some of them support functions as first class citizens, languages in that area are ecl (embbedabble common lisp IIRC), Gnu Smalltalk (gst) (Smalltalk has blocks), then there are libraries for "closures" e.g in glib2 http://library.gnome.org/devel/gobject/unstable/chapter-signal.html#closure which at least got near functional programming. So maybe using some of those implementations to do functional programming may be an option.
Well or you can go learning Ocaml, Haskell, Mozart/Oz or the like ;-)
Regards