[Haskell-beginners] Re: Encapsulation and Polymorphism

Drew Haven drew.haven at gmail.com
Thu Aug 26 02:45:12 EDT 2010


I think I found the answers to all my questions at
http://www.haskell.org/haskellwiki/Existential_type


Drew Haven
drew.haven at gmail.com



On Wed, Aug 25, 2010 at 9:43 PM, Drew Haven <drew.haven at gmail.com> wrote:
> I thought I was getting pretty good at Haskell, until I ran into this
> problem.  It's basically a way of maintaining encapsulation of data
> and functions while being able to have a container, such as a list,
> hold these items without worrying about their differences.  In OO this
> would be pretty easy.  You'd have an interface of some sort and the
> container would hold objects of the interface type.  Then you could
> write the individual classes that implement that interface in their
> own files and the main program wouldn't have to know about the
> details.
>
> I'm trying to write a silly little life simulator.  We've got a world
> where many different entities exist.  These entities are all
> different, but for a start we'll use fish and shark.  The fish like to
> swim around in the sea, and will manage to eat some amount of the
> overall food supply. The amount they consume from year to year varies
> and they have to consume a minimum amount every year or they die.
> When they've eaten enough food and have build up a surplus, they'll
> reproduce, but if they're starving they can't. Sharks are similar,
> except their food supply is the fish.  The two are very similar, but
> do slightly different things.  And every fish or shark has to keep
> track of its own amount of consumed food.
>
> If I wanted to define these separately, the best I've come up with is
> to create a data type that has all the information necessary, then
> somewhere maintain a master algebraic data type which wraps each of
> these in a type constructor.  I can then write another head for a
> general function that will match that type constructor and describe
> the behavior.
>
> I tried something clever with closures, but it turned out to be too
> clever for me and I couldn't avoid infinite types.
>
> Thoughts?
>
> Drew Haven
> drew.haven at gmail.com
>


More information about the Beginners mailing list