[Haskell-beginners] How to model this in haskell, get rid of my OO thinking?

Isaac Dupree ml at isaac.cedarswampstudios.org
Mon May 17 23:31:56 EDT 2010


On 05/17/10 23:21, haskell at lonely-star.org wrote:
> So I guess, it comes down to the questions: How would you model the
> scenario described above in haskell?

how about,

data Position = Position { positionX :: Float, positionY :: Float }

move :: Position -> (Float, Float) -> Position

data Spaceship = Spaceship Position ...other data members...
data Rocket = Rocket Position ...other data members...
(or perhaps not separate data-types but rather
data SpaceThing =
    Spaceship { spacePosition :: Position, ...other data members...}
  | Rocket { spacePosition :: Position, ...other data members...}
  ...

-- this latter has advantages and disadvantages regarding code reuse and 
code reliability.  Experiment!


-Isaac


More information about the Beginners mailing list