[Haskell-cafe] Functions are first class values in C
bf3 at telenet.be
Sat Dec 22 07:55:44 EST 2007
Actually, the C/C++ programmers I know think nothing can be done with functional programming languages because they are sooooo used the concept of a "C" function, that they think that Haskell functions are just that, functions like in C.
C# does not have function pointers, only the concept of a delegates (which was copied from Delphi's "closures", the official name?), and that is much closer to FP's "functions"
If people would see that Haskell functions are *not* that silly kind of C functions, but actually little "objects" that *can* carry immutable state, they would look differently at functional programming languages I guess.
So I think the word "function" means different things depending to which community you talk...
Before I knew Haskell, the OO community started to embrace the concept of "interfaces" more and more (aka purely abstract classes). Furthermore in OO, many bugs are caused IMO by keeping track of mutable state and caches (which is often premature optimization). So for complicated tasks, I tended to use more and more immutable objects, e.g. objects that could be constructed once, but not mutated. And then I noticed that it was often not needed to precompute all the values that got passed to the constructor, so I added C# "properties" that computed the inner cached values once, lazily.
IMO Haskell embraces the above ideas and much more, with the difference it encapsulates these ideas nicely and concisely, so you need only a fraction of the lines of code :)
So I guess if an OO programmer gets at this level, he should *not* look at Haskell, because he will not want to go back to his messy imperative programming languages :)
From: haskell-cafe-bounces at haskell.org [mailto:haskell-cafe-bounces at haskell.org] On Behalf Of Jules Bean
Sent: Saturday, December 22, 2007 1:09 PM
To: Cristian Baboi
Cc: haskell-cafe at haskell.org
Subject: Re: [Haskell-cafe] Functions are first class values in C
Cristian Baboi wrote:
> Let me show you an example to prove it.
That's not C.
That's the C preprocessor, which is a textual substitution macro
language. Macros certainly aren't first class (you can't pass a macro to
a function, only its expansion).
C does support function pointers, which are something like first class
functions. The main things C lacks which people associate with true
first-class function is:
The ability to construct anonymous/local functions.
The ability to capture local variables and return a function with some
The ability to write type-safe functions with polymorphic arguments.
Haskell-Cafe mailing list
Haskell-Cafe at haskell.org
More information about the Haskell-Cafe