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

Heinrich Apfelmus apfelmus at quantentunnel.de
Mon Mar 2 06:10:12 EST 2009


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



More information about the Beginners mailing list