7 ms·
Interestingly, if you watch the Erlang: The Movie (https://www.youtube.com/watch?v=BXmOlCy0oBM https://www.youtube.com/watch?v=BXmOlCy0oBM) they use the term 'd
by rkangel 6y ago
Interestingly, if you watch the Erlang: The Movie (https://www.youtube.com/watch?v=BXmOlCy0oBM https://www.youtube.com/watch?v=BXmOlCy0oBM) they use the term 'declarative programming' to refer to what we would now call 'functional programming'. It's an interesting way of thinking about it - these sort of declarative interfaces are very simple in languages with first class function support.
- tsimionescu 6y agoC has first-class functions support, but I don't think it's very easy to define such interfaces in C. Closures and automatic memory management seem to be the "magic dust" that makes this nice to use, first-class functions are necessary but not sufficient.
- lmm 6y agoC doesn't have first-class functions, because you can't define new functions in general places (only at top level).
- tsimionescu 6y agoThe definition of first-class functions is the ability to treat functions as data, which C supports. Yes, they are cumbersome to work with, since they must always be defined at top level, but that is just missing syntax sugar. GCC even allows nested function definitions.
- dllthomas 6y agoMy understanding agreed with your respondent. I haven't dug deeper but per wikipedia there's a split in the CS community as to whether function literals are necessary for functions to be first class. I think we all agree on the reality of the situation, whichever way we decide that ambiguity.
- lmm 6y ago> Yes, they are cumbersome to work with, since they must always be defined at top level, but that is just missing syntax sugar. It's not just syntax sugar. Try writing a function that takes an array of integers and an integer x, and sorts the array mod x by calling qsort. In C this is not just cumbersome but impossible.
- tsimionescu 6y agoWe can do it with a global variable. static int reg; int cmp(int a, int b) { return a%reg - b%reg; } void qsortModX(int[] a, size_t len, int x) { int tmp = reg; reg = x; qsort(a, len, sizeof(*a), cmp); reg = tmp; } We could even wrap qsort so that you could pass something much closer to a closure to it : static void* g_ctx; static int(*g_cmp)(void*, const void*, const void*) int compare(const void* a, const void* b) { return g_cmp(g_ctx, a, b) ; } void qsort_cls(void*[] a, size_t len, size_t elem, int(*cmp)(void*, const void*, const void*), void* ctx) { void* tmp = g_ctx; g_ctx = ctx; int(*tmp_f)(void*, const void*, const void*) = g_cmp; g_cmp = cmp; qsort(a, len, sizeof(*a), compare); g_ctx = tmp; g_cmp = tmp_f; } With this, you can define a `struct closure` that encapsulates a function and some data and use that to pass to qsort_cls. You can even make it thread safe by using thread local variables instead of globals. Basically, if you want higher-order functions in C, you can do it, but you need to take a context pointer and a function which accepts a context pointer. The writers of qsort didn't think of that, so we had to resort to global variables to do it, but you could also re-write qsort to avoid the need for the global variable. As I said, we're missing a lot of syntax sugar, but we can still work with functions as first class objects in pure C.
- lmm 6y agoWhat you're proposing requires extra work to be threadsafe, is even less typesafe than normal C functions (you've lost checking that `ctx` is actually an `int`), requires you to reimplement it for each standard function you want to use, and is significantly syntactically more cumbersome even after you've done all that. If that's "first class" then how bad would things have to get before you called them "second class"?
- deleted 6y ago[deleted]
- imglorp 6y agoProlog was inspiration for the Erlang syntax and it's about as declarative as you can get. In Prolog, everything is either a fact or a rule involving facts. If you leave some variables unbound, you get what we think of as a program, where the system tries to infer values.
- swyx 6y agoi think of declarative and functional vs imperative and nonfunctional as kind of the same thing. is there a word for nonfunctional?