[Haskell-cafe] Wikipedia on first-class object

Yitzchak Gale gale at sefer.org
Thu Dec 27 05:40:22 EST 2007


I wrote:
>> On the other hand, functions are members of types
>> that are just like any other Haskell type. They are
>> first-class in that sense.

Cristian Baboi wrote:
> I guess that would apply to any typed language.

Perhaps. But for many typed languages, it is not
practical to use. There may be problems with type
safety, or it may create a program that would
be considered hard to understand.

For example, in Haskell, you create a list of functions just
the same way you create a list of anything else.
You can then map a value across the functions, or
fold the functions together with the composition
operator, etc. These are normal, clear idioms
in Haskell that could easily appear in any simple
program. That is also true for some other functional
programming languages, but it is rare for
imperative ones.

Sometimes there is a particular style within a language
that works something like this. For example,
you can use C++ with STL that way in a certain sense.

Anyway, that is what I think it means when we say that
functions are first-class in Haskell.

>> Like any type, only certain operations make
>> sense on functions. Strings can be compared to each
>> other for equality and written to a disk, and you
>> can take the logarithm of a float, but none of those
>> operations make sense for functions. In particular,
>> two functions are equal only if they produce
>> the same value for every input, and in general it is
>> impossible for a computer to check that.

> Yes, but one can store the result of an operation to disk except in the
> particular case the result happen to be a function.

No, you can only store the result of an operation to
disk in the particular case that the result type represents
a list of bytes. Otherwise, you have to serialize it first.

Happily, Haskell has some cool tools for easily creating serialization
methods. And there are a number methods that are already
provided in the libraries for many types and for many
uses - the Read and Show classes are just one example.

But it is not clear at all how you could define a general
serialization method for functions. If you can come up
with one, please post it to Hackage. :)

> I'm not sure that in Haskell one can say that storing a value of some type
> to disk is an operation defined on that type.

It is. For example:

hPutStr :: Handle -> String -> IO ()

The result type of this function is IO (), which means an
IO action. In this case, the semantics of the action are
that, when performed, it writes the bytes into a file.

Regards,
Yitz


More information about the Haskell-Cafe mailing list