[Haskell-cafe] [C][enums][newbie] What is natural Haskell representation of such enum?

Mike Burns mike at mike-burns.com
Tue Jan 24 05:07:17 CET 2012

On 2012-01-23 13.45.50 -0800, David Barbour wrote:
> If your classes are more like `interfaces`, you could use Typeclasses to
> model them. Otherwise, look into OOHaskell. But I think your program
> architecture will simply be different in idiomatic Haskell than in
> idiomatic C++.

If your OO is very design patterned, and especially if it prefers
composition over inheritence, you can port it sorta directly, sometimes.

For example, all the classes that implement an interface become a sum
type, and their methods are functions that take a value of the sum type.

    interface MusicCompilation { def trackListing() : [Song] }
    class Record implements MusicCompilation { ... }
    class BlogPost implements MusicCompilation { ... }

Could translate to

    data MusicCompilation = Record [Song] | BlogPost [Song]

    trackListing (Record xs) = xs
    trackListing (BlogPost xs) = xs

The more your OO looks like C, the harder this will be.

More information about the Haskell-Cafe mailing list