[Haskell-beginners] Algebraic types, shared parameters, pattern matching

Christopher Howard christopher.howard at frigidcode.com
Thu Nov 1 03:36:54 CET 2012


I noticed that with algebraic types I can do something like so:

code
--------
data Projectile = Bullet { range :: Double }
                  | Missile { range :: Double }
                  | Torpedo { range :: Double }

f p = let r = range p in
      ...whatever...

--------

(This is interesting in and of itself, as I don't in this case have to
do pattern matching for each constructor. Also, apparently not all the
constructors actually need to have a "range" selector for the program to
compile!)

However, what if I wanted to make the same function f (again, without
multiple lines of pattern matching) but using a "simpler" data type like so:

code:
--------
type Range = Double

data Projectile = Bullet Range | Missile Range | Torpedo Range
--------

My first try was

code
--------
f (_ r) = ...
--------

But that doesn't work.

-- 
frigidcode.com
indicium.us

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 551 bytes
Desc: OpenPGP digital signature
URL: <http://www.haskell.org/pipermail/beginners/attachments/20121031/ea6fcfc6/attachment.pgp>


More information about the Beginners mailing list