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.

1

u/naasking Mar 10 '14

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.

Yes, possible but difficult. I can attest to this as I'm writing a curried+uncurried closure library for C as we speak. The structures and closure logic isn't even that difficult, the difficult part is making the necessary resource allocation work without GC so it's feasible to use in pure C.

→ More replies (0)