TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Functional programming in C

23 pointsby rudenoiseover 15 years ago

7 comments

demallienover 15 years ago
Of course, I can't help thinking of Greenspun's Tenth Rule when I see this sort of thing.<p>That said, my day job is C programming, and I am of course a heavy user of function pointers (how else can you get anything interesting done in C?). But C's type system does tend to get in the way, so you end up casting all pointers to void*, and things like that, just to shut the compiler up. Some Lisp-style macros would certainly help with this!
ajrossover 15 years ago
This seems to be kinda missing the point. Yes, you can do functional programming in almost anything that supports recursion and function indirection. But the essence of functional programming is <i>not</i> simply using a function pointer for everything. Those "if and switch statements" are perfectly functional concepts. It's the avoidance of side effects that generally distinguishes "functional" idioms from imperative ones.
akkartikover 15 years ago
Functional? If anything this is a poor man's OO, simulating a vtable. Even if it is using higher-order functions, that seems like a more accurate analogy.
wlievensover 15 years ago
It's not functional programming if you don't have nested functions and/or closures, is it?
评论 #1035639 未加载
评论 #1034612 未加载
评论 #1034820 未加载
DrJokepuover 15 years ago
Invoking functional pointers (call eax) is very very slow. If you're using C chances are you're using it because you need the extra performance.
评论 #1034673 未加载
pan69over 15 years ago
<a href="http://www.newty.de/fpt/index.html" rel="nofollow">http://www.newty.de/fpt/index.html</a>
wendroidover 15 years ago
Lots of the plan9 code is written in a style like this but with enums<p>A bit tricky to find the best example on my phone while on the bus but here's a flavour, the in-memory file system<p><a href="http://plan9.bell-labs.com/sources/plan9/sys/src/cmd/ramfs.c" rel="nofollow">http://plan9.bell-labs.com/sources/plan9/sys/src/cmd/ramfs.c</a><p>Edit : can anyone see how to add a comment to the article, I wanted to share the above info with him but comments don't seem open to me now I'm at my desk.<p>The plan9 way uses an interface style, the sort that is made explicit in Go :<p>from <a href="http://plan9.bell-labs.com/sources/plan9/sys/src/cmd/nntpfs.c" rel="nofollow">http://plan9.bell-labs.com/sources/plan9/sys/src/cmd/nntpfs....</a><p><pre><code> Srv nntpsrv = { .destroyfid= fsdestroyfid, .attach= fsattach, .clone= fsclone, .walk1= fswalk1, .open= fsopen, .read= fsread, .write= fswrite, .stat= fsstat, }; </code></pre> These file systems all run in user mode btw (as can all the disk based ones). Mycrotiv has even done a kernel with a built in rc shell that you attach your file systems after booting as yourself - a disk failure will not take down your system!
评论 #1034689 未加载