[Haskell-beginners] Exporting constructors
Brent Yorgey
byorgey at seas.upenn.edu
Thu Mar 24 16:48:42 CET 2011
On Thu, Mar 24, 2011 at 08:17:16AM -0400, Patrick LeBoutillier wrote:
> Hi all,
>
> Is it possible to export constructors from a module in a way that they
> can be used for pattern matching but not for creating types?
I would suggest exporting an 'eliminator' function for your data type
(like the functions 'maybe' and 'either'), which essentially allows the
user to do pattern-matching (although not with built-in pattern
matching syntax). For example, if your data type is
data Foo = Bar Int
| Baz Foo Char
| Quux
then you could export a function
matchFoo :: Foo -> (Int -> a) -> (Foo -> Char -> a) -> a -> a
which they could use like
matchFoo f
(\i -> ...) -- Bar case
(\f' c -> ...) -- Baz case
... -- Quux case
-Brent
More information about the Beginners
mailing list