How to understand `|` in this code snippet ?

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` ? Sincerely! ----- fac n = let { f = foldr (*) 1 [1..n] } in f -- View this message in context: http://old.nabble.com/How-to-understand-%60%7C%60-in-this-code-snippet---tp2... Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

On Sat, Feb 27, 2010 at 5:07 PM, zaxis
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 part of a case expression, see http://www.haskell.org/onlinereport/exps.html#sect3.13 lee

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). -- brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery@kf8nh.com system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH

On Sat, Feb 27, 2010 at 9:29 AM, Brandon S. Allbery KF8NH
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#pa...

Then can i change it to : case timeout of Just str -> do [(t, _)] <- reads str addTimeout t (hPutStrLn stderr "*** TIMEOUT" >> _exit 1) return () _ -> return () Sincerely! Brandon S. Allbery KF8NH 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).
-- brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery@kf8nh.com system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
----- fac n = let { f = foldr (*) 1 [1..n] } in f -- View this message in context: http://old.nabble.com/How-to-understand-%60%7C%60-in-this-code-snippet---tp2... Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

Am Sonntag 28 Februar 2010 02:08:18 schrieb zaxis:
Then can i change it to : case timeout of Just str -> do [(t, _)] <- reads str addTimeout t (hPutStrLn stderr "*** TIMEOUT" >> _exit 1) return () _ -> return ()
Sincerely!
No. The "| [(t,_)] <- reads str" in case timeout of Just str | [(t,_)] <- reads str -> ... is a "pattern guard", not a monadic bind (and where "p <- reads str" is a monadic bind, it's in the list monad). You can change it to case timeout of Just str -> case reads str of [(t,_)] -> addtimeout (hPutStrLn stderr "*** TIMEOUT" >> _exit 1) _ -> return () _ -> return () but why would you?
Brandon S. Allbery KF8NH 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).
-- brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery@kf8nh.com system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH

thanks! case timeout of Just str -> case reads str of [(t,_)] -> addtimeout (hPutStrLn stderr "*** TIMEOUT" >> _exit 1) _ -> return () _ -> return () is VERY clear! Daniel Fischer-4 wrote:
Am Sonntag 28 Februar 2010 02:08:18 schrieb zaxis:
Then can i change it to : case timeout of Just str -> do [(t, _)] <- reads str addTimeout t (hPutStrLn stderr "*** TIMEOUT" >> _exit 1) return () _ -> return ()
Sincerely!
No. The "| [(t,_)] <- reads str" in
case timeout of Just str | [(t,_)] <- reads str -> ...
is a "pattern guard", not a monadic bind (and where "p <- reads str" is a monadic bind, it's in the list monad). You can change it to
case timeout of Just str -> case reads str of [(t,_)] -> addtimeout (hPutStrLn stderr "*** TIMEOUT" >> _exit 1) _ -> return () _ -> return ()
but why would you?
Brandon S. Allbery KF8NH 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).
-- brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery@kf8nh.com system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
----- fac n = let { f = foldr (*) 1 [1..n] } in f -- View this message in context: http://old.nabble.com/How-to-understand-%60%7C%60-in-this-code-snippet---tp2... Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

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.
participants (6)
-
Ben Millwood
-
Brandon S. Allbery KF8NH
-
Daniel Fischer
-
Felipe Lessa
-
Lee Duhem
-
zaxis