12

In Haskell, I often do something like this:

f $ \x -> case x of
            A a1 a2 -> ...
            B b1 b2 -> ...
            C c1 c2 -> ...

But I don't want x, I just want to deconstruct it.

In Standard ML I can do something like this:

f (fn A(a1,a2) => ...
    | B(b1,b2) => ...
    | C(c1,c2) => ...)

Is there a way to do this in Haskell or with any GHC extensions?

og_loc
  • 173
  • 4

1 Answers1

21

You can use the LambdaCase language extension and perform

{-# LANGUAGE LambdaCase #-}
... 
f $ \case 
     A a1 a2 ->
...

as per your example.

You can read more about it in GHC's documentation

Arnon
  • 2,237
  • 15
  • 23
  • word I just scrolled through chapter 7 of the manual before making the question and I must of missed this cus I'm too gangsta! – og_loc Feb 23 '15 at 20:31