This actually does do the work, although I don't understand why an extra indentation is not needed. The "do" gives me a big surprise. Hal Daume III <hdaume@ISI.EDU> To: Ben_Yu@asc.aon.com cc: haskell@haskell.org 04/30/2004 10:54 Subject: Re: [Haskell] return? AM You can also do something like: myfunction = do ... if somecondition then return 5 else do more stuff here if someothercondition then return 6 else do more stuff here return 7 which will do what you want On Fri, 30 Apr 2004 Ben_Yu@asc.aon.com wrote:
Thanks you guys. I really love this mail list. Can always learn nice tips from experienced people here. :-)
I guess I'll go with the guard suggestion. ContT is nice, but don't want
to
introduce too much extras to the program just for syntax reason.
I do agree with you, Graham. Actually do do earlyreturn 1 return 2 return 3
is equivalent to:
do earlyreturn 1 return 2 return 3
While the first should be 3 by intuition. the second should be 1.
I guess that's the reason why imperative return is not possible in monad.
Graham Klyne
<GK@ninebynine.or To:
Ben_Yu@asc.aon.com, haskell@haskell.org
g> cc:
Subject: Re: [Haskell]
return?
04/30/2004 04:45
AM
Is this possible at all?
I don't think so, in the form that you suggest.
Ultimately, it all comes down to function applications, for which there
is
no such "bail out". Rather, I think something like this is required:
do { ... ; if cond then return 1 else do (the rest) }
Here's an example from some real (tested) code: [[ -- Open and read file, returning its handle and content, or Nothing -- WARNING: the handle must not be closed until input is fully evaluated repOpenFile :: String -> RepStateIO (Maybe (Handle,String)) repOpenFile fnam = do { (hnd,hop) <- lift $ if null fnam then return (stdin,True) else do { o <- try (openFile fnam ReadMode) ; case o of Left e -> return (stdin,False) Right h -> return (h,True) } ; hrd <- lift $ hIsReadable hnd ; res <- if hop && hrd then do { ; fc <- lift $ hGetContents hnd ; return $ Just (hnd,fc) } else do { lift $ hClose hnd ; repError ("Cannot read file: "++fnam) 3 ; return Nothing } ; return res } ]]
#g --
Hi, While writing monad programs, I sometimes want to do a return as it is in imperative program. i.e., do{return 1; return 2} is same as return 1
This seems useful to me when I need to do something like do mwhen cond $ return 1 ...... -- subsequent actions
I know I can do if cond then return 1 else ( ...--subsequent actions )
However, that syntax does not look very pleasant to me due to this extra indentation and the pair of parens.
Is this possible at all?
Ben.
_______________________________________________ Haskell mailing list Haskell@haskell.org http://www.haskell.org/mailman/listinfo/haskell
------------ Graham Klyne For email: http://www.ninebynine.org/#Contact
_______________________________________________ Haskell mailing list Haskell@haskell.org http://www.haskell.org/mailman/listinfo/haskell
-- Hal Daume III | hdaume@isi.edu "Arrest this man, he talks in maths." | www.isi.edu/~hdaume