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

Tom Poliquin poliquin at softcomp.com
Tue Mar 3 02:51:03 EST 2009


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



More information about the Beginners mailing list