[Haskell-beginners] Re: Finite State Machine ..

Tom Poliquin poliquin at softcomp.com
Mon Mar 2 01:28:27 EST 2009


> > 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




More information about the Beginners mailing list