To answer your questions:
It would be possible to define Vertex2 to take two types, to allow X to have one type and Y another, e.g.
data Vertex2 xtype ytype = Vertex2 xtype ytype
. However, it's generally a bad idea to have two different types for X and Y so instead it's defined as:data Vertex2 sametype = Vertex2 sametype sametype
to save problems.Since you have explicitly typed the Vertex2 in your declaration for Particle, you don't need to type the expression you list. Just
Particle { pos = Vertex2 1 0 }
should be enough. (Or:Particle (Vertex2 1 0)
).You get the compile error because you don't need monadic bind.
part
has typeParticle
, notIO Particle
, so you don't need bind to get the value out. You can write either:drawParticle part = let Vertex2 x y = pos part in print x
or:
drawParticle part = do
let Vertex2 x y = pos part
print x
(Note the two different forms of let, depending on whether it's in a do
block; this confused me when I started out.)