I'm working on a project (in Haskell) and was given some old Java code to indicate the required functionality for a particular function. It's a page of nested (4 deep) if statements. (That's probably why they gave me the code, no one could describe it). I would normally convert this to an FSM, ..... but this is Haskell! So, 1) Is there a nice (canonical) way of eliminating nested evil in Haskell? I thought that perhaps making a tuple of all the if's conditions and patterm matching on them might make a bit more comprehensible. Likely there's a better way. 2) If an FSM is appropriate is there a 'standard' Haskell FSM implementation? I looked around and could find very little. One paper talked about Arrows but that seems like a bit of overkill .. It actually seems like a fun problem .. if I had the time .. Any thoughts greatly appreciated! Tom |
This does sound interesting. Can you provide (at least some of) the code?
On Sat, Feb 28, 2009 at 9:36 PM, Tom Poliquin <[hidden email]> wrote: > > I'm working on a project (in Haskell) and was > given some old Java code to indicate the required > functionality for a particular function. It's a page of > nested (4 deep) if statements. (That's probably > why they gave me the code, no one could > describe it). > > I would normally convert this to an FSM, > ..... but this is Haskell! > > So, > > 1) Is there a nice (canonical) way of eliminating > nested evil in Haskell? I thought that perhaps making > a tuple of all the if's conditions and patterm matching > on them might make a bit more comprehensible. > Likely there's a better way. > > 2) If an FSM is appropriate is there a 'standard' > Haskell FSM implementation? > I looked around and could find very little. One > paper talked about Arrows but that seems like a bit > of overkill .. > > It actually seems like a fun problem .. if I > had the time .. > > Any thoughts greatly appreciated! > > > Tom > _______________________________________________ > Beginners mailing list > [hidden email] > http://www.haskell.org/mailman/listinfo/beginners > An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20090228/4b918e51/attachment.htm |
In reply to this post by Tom Poliquin
Tom Poliquin wrote:
> > 1) Is there a nice (canonical) way of eliminating > nested evil in Haskell? I thought that perhaps making > a tuple of all the if's conditions and patterm matching > on them might make a bit more comprehensible. > Likely there's a better way. You probably want guards, like this fib n | n == 0 = 0 | n == 1 = 1 | otherwise = fib (n-1) + fib (n-2) Regards, apfelmus -- http://apfelmus.nfshost.com |
> > I'm working on a project (in Haskell) and was
> > given some old Java code to indicate the required > > functionality for a particular function. It's a page of > > nested (4 deep) if statements. (That's probably > > why they gave me the code, no one could > > describe it). > > 1) Is there a nice (canonical) way of eliminating > > nested evil in Haskell? > >2) If an FSM is appropriate is there a 'standard' > > Haskell FSM implementation? > > It actually seems like a fun problem .. if I > > had the time .. Andrew Wagner wrote: > This does sound interesting. Can you provide (at least some of) the code? As I thought it would violate the group mores I didn't include the Java code here .. :-) It's at http://www.softcomp.com/pastes/ifexample.java This is a typical module .. others are worse. Heinrich Apfelmus wrote: > You probably want guards, like this > fib n > | n == 0 = 0 > | n == 1 = 1 > | otherwise = fib (n-1) + fib (n-2) Is this what you had in mind? module Main where foo a b c | p1 && p2 || not p3 = 42 | p1 || not p2 && p3 = 13 | otherwise = 0 where -- setup if predicates p1 = a > b p2 = a + b > 2 p3 = a - b < 1 main = do x <- return $ foo 9 2 3 print x -- 42 Thanks, Tom |
Tom Poliquin wrote:
> Heinrich Apfelmus wrote: >> >> You probably want guards, like this >> >> fib n >> | n == 0 = 0 >> | n == 1 = 1 >> | otherwise = fib (n-1) + fib (n-2) > > Is this what you had in mind? > > module Main where > > foo a b c > | p1 && p2 || not p3 = 42 > | p1 || not p2 && p3 = 13 > | otherwise = 0 > where > -- setup if predicates > p1 = a > b > p2 = a + b > 2 > p3 = a - b < 1 > > main = do > x <- return $ foo 9 2 3 > print x > > -- 42 Yes. Of course, it will become clunky very quickly; only abstraction and insights into the problem domain can help in these cases. Regards, apfelmus -- http://apfelmus.nfshost.com |
Heinrich Apfelmus wrote: > Tom Poliquin wrote: > > Heinrich Apfelmus wrote: > >> You probably want guards, like this > >> > >> fib n > >> > >> | n == 0 = 0 > >> | n == 1 = 1 > >> | otherwise = fib (n-1) + fib (n-2) > > > > Is this what you had in mind? > > > > module Main where > > > > foo a b c > > > > | p1 && p2 || not p3 = 42 > > | p1 || not p2 && p3 = 13 > > | otherwise = 0 > > > > where > > -- setup if predicates > > p1 = a > b > > p2 = a + b > 2 > > p3 = a - b < 1 > > > > main = do > > x <- return $ foo 9 2 3 > > print x > > > > -- 42 > > Yes. > > Of course, it will become clunky very quickly; only abstraction and > insights into the problem domain can help in these cases. ay, there's the rub! (Hamlet Act III, Scene I) Those pesky insights into the problem ! If I have time I'll try all three approaches; guards,FSM, and insights. Thanks everyone for the help! Tom |
Free forum by Nabble | Edit this page |