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

Karl Voelker ktvoelker at gmail.com
Thu Nov 1 08:05:45 CET 2012


On Wed, Oct 31, 2012 at 7:36 PM, Christopher Howard
<christopher.howard at frigidcode.com> wrote:
> (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!)

I would avoid using this "feature" of the language, because non-total
functions are a hassle.

> code:
> --------
> type Range = Double
>
> data Projectile = Bullet Range | Missile Range | Torpedo Range
>
> f (_ r) = ...
> --------

Nope, you can't do that. But you can do this:

data ProjectileType = Bullet | Missile | Torpedo

data Projectile = Projectile ProjectileType Range

f (Projectile _ r) = ...

-Karl



More information about the Beginners mailing list