[Haskell-cafe] MultiCase alternative

Baa aquagnu at gmail.com
Fri Jun 16 05:43:46 UTC 2017


Hello, Richard.

As a result I did with "...where f c = ..." :) A way with "which" is
interesting but duplicates unique constructors (A -> A', B -> B').

Interesting, is F# can solve this problem with active pattern?

Pattern matching in Haskell does not seem enought flexible, for
example, if I have `data D = D a b c d`, how can I match it without
placeholders for `D` args? Something like `D...`? I need to write
`D _ _ _ _` if I want to match without to bind args, right?

I want to say, that may be (I'm not sure, I'm newbie in Haskell) active
patterns can solve this and many other problems, may be active-pattern
+ reflection. For last example, I create pattern `IsD` in place where
`D` is defined and use it - to be more independent on D args (D can be
record and I can use getters/lens only to access its args, so I need a
way to be similar independent from its args in pattern-matching too).

But again, I'm not sure about right approaches - I'm newbie yet.


===
Best regards, Paul

> 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
> 
> then
>     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.
> 
> _______________________________________________
> Haskell-Cafe mailing list
> To (un)subscribe, modify options or view archives go to:
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
> Only members subscribed via the mailman list are allowed to post.


More information about the Haskell-Cafe mailing list