[Haskell-cafe] proposal: point free case expressions

Tim Newsham newsham at lava.net
Thu Nov 5 13:20:20 EST 2009


> Wouldn't it be nice if we could write point free case statements?

Yes.  Haskell should automatically define destructors for
each data type you define in addition to defining constructors.

> I regularly find myself writing down something like this:
>
>> myFunc = anotherFunc $ \x -> case x of
>>                                Left err -> print err
>>                                Right msg -> putStrLn msg

When you define "data Either a b = Left a | Right b"
it should define "Left", "Right" and "destruct_Either" (or
whatever other name you want to give it).  Of course
"destruct_Either" is just "either" from the prelude, and
you also get "maybe", but these shouldn't be prelude
functions, they should be automatically derived.  Then your
points free version is just:

    myFunc = anotherFunc $ either print putStrLn

In the meantime, the Data.Derive library
http://community.haskell.org/~ndm/derive/
has a function for deriving these using TH:
http://hackage.haskell.org/packages/archive/derive/latest/doc/html/Data-Derive-Fold.html

This derives a non-recursive definition for recursive data types.
Its also possible to derive a similar recursive definition for
recursive data types.

> Sebastiaan Visser

Tim Newsham
http://www.thenewsh.com/~newsham/


More information about the Haskell-Cafe mailing list