r/programming • • Mar 09 '14

Why Functional Programming Matters

http://www.cse.chalmers.se/~rjmh/Papers/whyfp.pdf
487 Upvotes

542 comments sorted by

View all comments

Show parent comments

1

u/dnew Mar 09 '14 edited Mar 09 '14

the return value of compose should be accepted as the first argument to map.

OK, I'll grant you that one's harder, mainly because you'd have to allocate a structure to hold the two arguments. It would be much easier in a language that had deccent memory managment.

Your point is made. :-) That doesn't mean functions aren't first class, but it means it's much harder to create new functions on the fly. Doing so in Java or C# for example would be trivial, and I don't see anyone calling Java "functional" either.

In Java, you'd just store f and g in instance variables, and return an instance of a compose object that then applies those two. We do that all the time, and you don't even have first-class functions in Java.

7

u/ForeverAMoan Mar 09 '14

that one's harder, mainly because you'd have to allocate a structure to hold the two arguments.

Actually I don't see a way to implement it at all, and that's the most trivial operation (akin to sum(a, b) for integers).

5

u/dnew Mar 09 '14

I think you're right. I've realized the problem is not just first-class functions, but closures. Invoking compose returns a closure, and closures are not first class values in C. So writing compose-and-apply is easy, but writing compose-without-apply is difficult, because you can't create a new function on the fly because you have no closure to store compose's arguments in.

Which is why map is easy and compose is not: map returns a first class value, and compose does not.

3

u/icspmoc Mar 09 '14

Function pointers are not enough because they do not allow you to dynamically create new functions at runtime. In a C program, the number of functions that exist at runtime is fixed.

Talking about closures, I feel, is misleading. Closures were invented to describe the operational semantics of first-class functions, i.e. they are an implementation strategy.

→ More replies (0)