[Haskell-beginners] type of pattern matching

Brent Yorgey byorgey at seas.upenn.edu
Wed Jul 7 05:15:56 EDT 2010


On Wed, Jul 07, 2010 at 08:26:50AM +0100, Stephen Tetley wrote:
> Hi Michael
> 
> Untested - maybe you can use Record puns (section 7.3.15.of the GHC
> manual). Though they might only work if you have used field names in
> your data type.
> 
> {} swaps for _ _ _ _ _ _  so it would be:
> 
> > doSomething :: Maybe Result
> > doSomething item = case item of
> >  note@(Note {}) -> Just $ process note
> >  _                     ->  Nothing

Nope, this works in general, whether you've used field names or not.
I use this trick all the time.  Also, I see no reason to use a case;
why not just

  doSomething item@(Note{}) = Just $ process item
  doSomething _             = Nothing

-Brent


More information about the Beginners mailing list