[Haskell-beginners] Further constraining types

Antoine Latter aslatter at gmail.com
Sun Aug 7 08:08:31 CEST 2011


On Sun, Aug 7, 2011 at 12:54 AM, Christopher Howard
>
> One more quick question: If I hide my actual constructors in favor of a
> smart constructor, is it no longer possible for me to do direct pattern
> matching on the values? E.g., the compiler does not allow this:
>
> analyze (Natural 5) = "It's a five!!!"
>
> ...so I have to do this:
>
> analyze a | natural 5 = "It's a five!!!"

You can use non-standard GHC extensions:

Given a function:

someView :: Natural a -> a

you can pattern match like so:

case myNat of
  (someView -> 5) -> "It's a five!!!"

This requires {-# LANGUAGE ViewPatterns #-} at the top of your source
file to enable.

Antoine

>
> --
> frigidcode.com
> theologia.indicium.us
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>



More information about the Beginners mailing list