[Haskell-cafe] MultiCase alternative
Clinton Mead
clintonmead at gmail.com
Fri Jun 16 05:55:04 UTC 2017
You can record match like so "x at D{}" but then you'll need someway to access
the contents of "x" (if you're interested in the contents, that is).
On Fri, Jun 16, 2017 at 3:43 PM, Baa <aquagnu at gmail.com> wrote:
> 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.
> _______________________________________________
> 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.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20170616/1736627f/attachment.html>
More information about the Haskell-Cafe
mailing list