[Haskell-cafe] matching constructors
Ben Rudiak-Gould
benrg at dark.darkweb.com
Mon Mar 8 12:55:26 EST 2004
On Mon, 8 Mar 2004, Vadim Zaliva wrote:
> I am doing command line options parsing. I've defined Flag type with
> constructor
> for each possible option:
>
> data Flag = Verbose |
> Input String |
> Output String |
> Filter String
> deriving (Show, Typeable, Data)
>
> getOpt returns me a list of such objects. Now I need to
> look things up there by constructor. For example:
>
> ....
> doSomething fltflag
> where
> (Filter fltflag) = findFlag (Filter "none") opts
Try this instead:
doSomething $ option "none" [fltflag | Filter fltflag <- opts]
...
option :: a -> [a] -> a
option def [] = def
option def [x] = x
option def _ = error "Only one of each option allowed"
-- Ben
More information about the Haskell-Cafe
mailing list