Pattern guards vs. case

Malcolm Wallace Malcolm.Wallace@cs.york.ac.uk
Thu, 1 Mar 2001 09:13:44 +0000


> runRandom last max num
> | num > 1     = runRandom (fst new) max (num-1)
> | otherwise   = snd new

> What's the difference between the pipe-syntax, and a case statement,
> i.e. writing the function as

>    runRandom last max num = case num of
>       1 -> runRandom ....
>       otherwise -> snd new

Eeek!  This is not the same function!  (num>1)  =/=  (num==1)
Oh, and it isn't type-correct either.
Try:

     runRandom last max num = case num>1 of
        True -> runRandom ....
        otherwise -> snd new

Oops.  No.  Try again:

     runRandom last max num = case num>1 of
        True -> runRandom ....
        False -> snd new

That's better.  These transformations by hand can be tricky, huh?
Regards,
    Malcolm