How to express a logic matrix clearly?

Hi, I have trouble describing this clearly. Let me show code directly. data Rule1 = A1 | A2 | A3 data Rule2 = B1 | B2 | B3 foo a b = if a == A1 then if b == B1 then fun1 else if b == B2 then fun2 else if b == B3 then fun3 else fun4 ... Basically, Rule1 and Rule2 compose a matrix, for each case of Rule1 and Rule2, I need to do different things. Above is already long and not quite clear, and it is far from complete. So my question is, is there a way/lib that I can make this clear to read/understand?

This is a good opportunity to use case syntax. You can also case on tuples, like this: ``` foo a b = case (a,b) of (A1,B1) -> fun1 (A1,B2) -> fun2 (A1,B3) -> fun3 (A1,_) -> fun4 <- this is probably not necessary anymore, unless you expect Rule2 to get more constructors. (A2,B1) -> fun5 ... ``` On Thu, May 16, 2019 at 10:14 PM Magicloud Magiclouds < magicloud.magiclouds@gmail.com> wrote:
Hi, I have trouble describing this clearly. Let me show code directly.
data Rule1 = A1 | A2 | A3 data Rule2 = B1 | B2 | B3
foo a b = if a == A1 then if b == B1 then fun1 else if b == B2 then fun2 else if b == B3 then fun3 else fun4 ...
Basically, Rule1 and Rule2 compose a matrix, for each case of Rule1 and Rule2, I need to do different things. Above is already long and not quite clear, and it is far from complete.
So my question is, is there a way/lib that I can make this clear to read/understand? _______________________________________________ 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.
-- Nathan Bloomfield Code Wrangler, Team Absinthe Automattic, Inc.

Yes, that is a good way. I was thinking something like a "table".
Well, just realize that if it was a more dimension, it is hard to
express in 2-D text anyway....
On Fri, May 17, 2019 at 11:21 AM Nathan Bloomfield
This is a good opportunity to use case syntax. You can also case on tuples, like this:
``` foo a b = case (a,b) of (A1,B1) -> fun1 (A1,B2) -> fun2 (A1,B3) -> fun3 (A1,_) -> fun4 <- this is probably not necessary anymore, unless you expect Rule2 to get more constructors. (A2,B1) -> fun5 ... ```
On Thu, May 16, 2019 at 10:14 PM Magicloud Magiclouds
wrote: Hi, I have trouble describing this clearly. Let me show code directly.
data Rule1 = A1 | A2 | A3 data Rule2 = B1 | B2 | B3
foo a b = if a == A1 then if b == B1 then fun1 else if b == B2 then fun2 else if b == B3 then fun3 else fun4 ...
Basically, Rule1 and Rule2 compose a matrix, for each case of Rule1 and Rule2, I need to do different things. Above is already long and not quite clear, and it is far from complete.
So my question is, is there a way/lib that I can make this clear to read/understand? _______________________________________________ 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.
-- Nathan Bloomfield Code Wrangler, Team Absinthe Automattic, Inc.

On Thu, May 16, 2019 at 10:21:14PM -0500, Nathan Bloomfield wrote:
This is a good opportunity to use case syntax. You can also case on tuples, like this:
``` foo a b = case (a,b) of (A1,B1) -> fun1 (A1,B2) -> fun2 (A1,B3) -> fun3 (A1,_) -> fun4 <- this is probably not necessary anymore, unless you expect Rule2 to get more constructors. (A2,B1) -> fun5 ... ```
In fact, and this is veering somewhat off-topic, but I would strongly recommend *not* making a default match `(A1,_)` because then you will get an explicit warning if indeed `Rule2` gets more constructors.

For one, you should pattern match rather than using nested ifs, viz. foo A1 B1 = fun1 Using a case statement on tuples is not necessary. On 5/17/19 3:12 AM, Magicloud Magiclouds wrote:
Hi, I have trouble describing this clearly. Let me show code directly.
data Rule1 = A1 | A2 | A3 data Rule2 = B1 | B2 | B3
foo a b = if a == A1 then if b == B1 then fun1 else if b == B2 then fun2 else if b == B3 then fun3 else fun4 ...
Basically, Rule1 and Rule2 compose a matrix, for each case of Rule1 and Rule2, I need to do different things. Above is already long and not quite clear, and it is far from complete.
So my question is, is there a way/lib that I can make this clear to read/understand? _______________________________________________ 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.

This works, but may not suitable as case. Sometimes it may be hard to
name the function just for branching.
On Fri, May 17, 2019 at 11:25 AM Vanessa McHale
For one, you should pattern match rather than using nested ifs, viz.
foo A1 B1 = fun1
Using a case statement on tuples is not necessary.
On 5/17/19 3:12 AM, Magicloud Magiclouds wrote:
Hi, I have trouble describing this clearly. Let me show code directly.
data Rule1 = A1 | A2 | A3 data Rule2 = B1 | B2 | B3
foo a b = if a == A1 then if b == B1 then fun1 else if b == B2 then fun2 else if b == B3 then fun3 else fun4 ...
Basically, Rule1 and Rule2 compose a matrix, for each case of Rule1 and Rule2, I need to do different things. Above is already long and not quite clear, and it is far from complete.
So my question is, is there a way/lib that I can make this clear to read/understand? _______________________________________________ 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.
participants (4)
-
Magicloud Magiclouds
-
Nathan Bloomfield
-
Tom Ellis
-
Vanessa McHale