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

Ben Millwood haskell at benmachine.co.uk
Sat Feb 27 11:51:09 EST 2010


On Sat, Feb 27, 2010 at 9:29 AM, Brandon S. Allbery KF8NH
<allbery at ece.cmu.edu> wrote:
> On Feb 27, 2010, at 04:07 , zaxis wrote:
>>
>> xxxMain = do
>>   timeout <- getEnv "xxx_TIMEOUT"
>>   case timeout of
>>       Just str | [(t, _)] <- reads str -> do
>>           addTimeout t (hPutStrLn stderr "*** TIMEOUT" >> _exit 1)
>>           return ()
>>       _ -> return ()
>> .......
>>
>> What does the `|` mean in "Just str | [(t, _)] <- reads str" ?
>> Is it a logical `or` ?
>
> It's a guard.  Same as with function definitions (in fact, function
> definitions of that form are converted to case expressions).
>

In fact it seems to be a pattern guard, which (until recently) are
(were) a non-standard extension:
http://www.haskell.org/ghc/docs/latest/html/users_guide/syntax-extns.html#pattern-guards


More information about the Haskell-Cafe mailing list