[Haskell-cafe] MultiCase alternative

Richard A. O'Keefe ok at cs.otago.ac.nz
Thu Jun 15 23:31:32 UTC 2017

There is another elementary alternative.  If you need to treat C and
D the same in just one place, you don't really have a problem.  If
you need to treat them the same in several places, do this:

data T a b c = A a | B b | C c | D c  -- existing type

data Which a b c = A' a | B' b | CD Bool c

which :: T a b c -> Which a b c
which (A a) = A' a
which (B b) = B' b
which (C c) = CD False c
which (D c) = CD True  c

    case which $ x of
      A' a ->
      B' b ->
      CD _ c -> ...

If you want to merge the C and D cases often, I like this approach,
otherwise the
    C c -> f c
    D c -> f c
    where f c = ...
approach is better.

More information about the Haskell-Cafe mailing list