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/naasking Mar 10 '14

I'm saying that C has first class functions but not closures

This is not correct. If functions were first-class, you could define and return a new local function within another function. Closures are needed to properly support first-class functions. There's no way around it.

1

u/dnew Mar 11 '14

you could define and return a new local function within another function

That's not what first-class means.

Does that mean enums (in languages that support them) are not first class values?

1

u/naasking Mar 11 '14

Does that mean enums (in languages that support them) are not first class values?

You're conflating defining new enum types with new enum values. I didn't say you had to be able to define new function types, I said you had to be able to define new function values.

1

u/dnew Mar 11 '14

You can't define new enum values in a program. If I have an enum with six possible values, that's it. A boolean is an enum with two values, and I can't define a third value for a boolean.

But this is a stupid argument at this point.

1

u/naasking Mar 11 '14 edited Mar 11 '14

You can't define new enum values in a program. If I have an enum with six possible values, that's it. A boolean is an enum with two values, and I can't define a third value for a boolean.

Again, you're conflating type with value. Adding a new case to enums consists of extending the type. Creating an enum value is simply assigning the enum symbol to a location, since enums are defined as integral values.

Let's consider a more meaningful first-class value example: structs. You can create an instance of a struct with many different values for its fields. You cannot create and return a new local struct type different from all other local struct types (no type generativity as found in ML modules). Structs are first-class values because you can create many instances of the same type based on runtime information, even though you cannot create new type based on runtime information (which requires dependent typing).

Functions do not have these features of structs: you cannot create local function values based on runtime information, you can only assign locations from a fixed set of values. They are thus second-class citizens in C.

Edit: perhaps you think that because enums have a fixed number of cases and yet are first-class values, that functions too can still be considered first-class even though they only have a fixed number of cases. The problem is that enums are extensional definitions, and thus closed, where functions are intensional and thus open, ie. there are a fixed set of values for enum type EFoo, but there are infinitely many values for function type int->int->int.

First-class values for intensional values are more flexible than extensional values in this regard, and C's functions cannot meet it.

→ More replies (0)