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.
On 2004-04-29T18:27:32-0500, Ben_Yu@asc.aon.com wrote:
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
Hello, You can build an error monad transformer along the lines of the Control.Monad.Error module, in particular the ErrorT data type there (but without the Error class). You can then replace "return" above with "throwError" for your error monad transformer. Ken -- Edit this signature at http://www.digitas.harvard.edu/cgi-bin/ken/sig BBC News: Universities face week of protest http://news.bbc.co.uk/1/hi/education/3508209.stm
On Thu, Apr 29, 2004 at 06:27:32PM -0500, Ben_Yu@asc.aon.com wrote:
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
Is this possible at all?
Someone already proposed an Error monad, but I think that Continuation monad is more appropriate, as it's easier to ,,return'' values of different types. It also allows you to choose the place you want to ,,return'' to. You'll probably want to use features of other monad, so use a ContT monad transformer. Here's an example: import Control.Monad.Cont code = (\m -> runContT m return) $ do x <- callCC $ \exit -> do exit 1 return 2 lift (print x) This will print 1, not 2. Best regards, Tom -- .signature: Too many levels of symbolic links
On Thu, Apr 29, 2004 at 06:27:32PM -0500, Ben_Yu@asc.aon.com wrote:
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 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.
you can make things somewhat better with this construct foo = do baz if cond then return bar else do bua bam the lack of an early return seems like an ommision from the do syntax, but an understandable one. John -- John Meacham - ⑆repetae.net⑆john⑈
John Meacham wrote:
you can make things somewhat better with this construct
foo = do baz if cond then return bar else do bua bam
Except that this is invalid according to the Haskell report. In note 1 in section 9.3 (Layout), the report explicitly states that "A nested context must be further indented than the enclosing context". While hugs and ghc incorrectly accept your code, hbc and nhc98 get it right, i.e., they raise a parse error on it. Wolfgang
participants (5)
-
Ben_Yu@asc.aon.com -
Chung-chieh Shan -
John Meacham -
Tomasz Zielonka -
Wolfgang Lux