3 ms·
There's a common cliché, that C programs, as embodied in Unix, are probably the most widely used form of functional programming - or at least CSP. This example
by colmmacc 13y ago
There's a common cliché, that C programs, as embodied in Unix, are probably the most widely used form of functional programming - or at least CSP. This example is idiomatic;
grep -c foo * | uniq | sort -rn
It's more than possible to structure C programs this way internally too, elegant even. Use forks and pipes if you want to keep it simple, build co-routines if you're up for it. It's probably the most practical and beneficial way you can re-apply the lessons of FP to C.
But the book doesn't mention either technique - just weird and ugly tricks to emulate tail-recursion, cons et al, probably the least effective parts of FP to try to use in C.
- yp_master 13y ago"But the book doesn't mention either technique..." As other commenters are pointing out, the book's aim is to teach imperative programming with C to students of SML. It is not aimed at teaching functional programming in C. The technique you describe -- using what UNIX offers, e.g., pipe and fork -- is used a lot by djb. This book seems like a gentle intro to C. And there's nothing wrong with keeping things simple in the beginning. With respect to books and tutorials on C, I have seen much worse. Master the basics of C, then go read djb's code for lessons on how to structure programs and smartly utilise what UNIX has to offer. Keep K&R and Stevens nearby for reference. A classic example is passing file descriptors instead of using a pipe function that opens a shell. There is no book on C that teaches that, but it is elegant programming indeed. Even when one strives to keep things simple, C (and UNIX) have many gotchas. There's a lot to learn. This book doesn't overwhelm the reader. Personally, I like the approach. Lots of short consistent examples. Lucid writing style, too.
- e12e 13y agoThat section on linked lists (cons in c and sml) was really nice. I don't think I've ever seen such clear, plain c for working with singly linked lists (which even checks the return code for malloc!). I actually had to look up what K&R had on it, and there's a section on tree-structures -- but it ends up being cluttered (although that's justifiable in K&R -- the focus isn't just on simple example code). Disclaimer: I don't actually know C :-) [edit: link: http://eprints.eemcs.utwente.nl/1077/08/list.html http://eprints.eemcs.utwente.nl/1077/08/list.html ]
- X4 13y ago@colmmacc would you please mind submitting or sharing the link to the book, paper or ressource you think that teaches the elegant Advanced Functional C Programming techniques you mention? I'd definitely upvote it.