[Haskell-cafe] How to understand `|` in this code snippet ?

Felipe Lessa felipe.lessa at gmail.com
Sat Feb 27 20:46:48 EST 2010


On Sat, Feb 27, 2010 at 05:08:18PM -0800, zaxis wrote:
> Then can i change it to :
> case timeout of
>         Just str -> do
>             [(t, _)] <- reads str
>             addTimeout t (hPutStrLn stderr "*** TIMEOUT" >> _exit 1)
>             return ()
>         _ -> return ()

No, that's different.  You could change it to:

case timeout of
  Just str -> case reads str of
    [(t, _)] -> do addTimeout t (hPutStrLn stderr "*** TIMEOUT" >> _exit 1)
    	     	   return ()
    _        -> other -- (1)
  _        -> other -- (2)
where other = return ()

The cases (1) and (2) are the same and simulate the fact that
when the pattern guard fails, then execution falls to the next
case.

Of course you could just write

case fmap (reads str) timeout of
  Just [(t, _)] -> ...
  _             -> ...

HTH,

--
Felipe.


More information about the Haskell-Cafe mailing list