4 ms·
No, functions aren't first class in C. When you use a function in an expression it undergoes lvalue conversion and "decays" to a pointer to the function. You ca
by milesrout 2y ago
No, functions aren't first class in C. When you use a function in an expression it undergoes lvalue conversion and "decays" to a pointer to the function. You can only call, store, etc function pointers, not functions. Function pointers are first class. Functions are not as you can't create them at runtime.
A functional programming language is one with first class functions.
- grandempire 2y agoWhat is the impact on the user of having first class functions vs first class function pointers? Last I checked when you implement lambda in lisp it's also a pointer to the lambda internally.
- xigoi 2y agoFunction pointers can’t close over variables.
- vkazanov 2y agoThe other important bit here is garbage collection. Local and anonymous functions that capture lexical environments really, really work much better in languages built around GCs. Without garbage collection a trivial closure (as in javascript or lisps) suddenly needs to make a lot of decisions around referencing data that can be either on the stack or in the heap.
- grandempire 2y agoC++ does this and the decision to make is to capture by reference or value. Environments aren’t a thing in Haskell etc. does that mean it’s not functional?
- vkazanov 2y agoYes, C++ is a great example of having to make decisions that don't have good solutions without a GC or something like. See mentions of undefined behaviour in relevant sections of the standard, i.e. when a lambda captures something with a limited lifetime. Are you saying that Haskell doesn't have lexical environments? It very much does, just as all major languages of the ML language family do.
- grandempire 2y agoThat has nothing to do with the value/pointer distinction. And “close over” semantics differ greatly depending on the language.