[Haskell] return?

Ben_Yu at asc.aon.com Ben_Yu at asc.aon.com
Fri Apr 30 11:40:50 EDT 2004

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
    earlyreturn 1
    return 2
return 3

is equivalent to:

  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 at ninebynine.or        To:       Ben_Yu at asc.aon.com, haskell at haskell.org                                       
                      g>                       cc:                                                                                     
                                               Subject:  Re: [Haskell] return?                                                         
                      04/30/2004 04:45                                                                                                 

>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:

     { ...
     ; 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)
             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)
             do  { lift $ hClose hnd
                 ; repError ("Cannot read file: "++fnam) 3
                 ; return Nothing
         ; return res


>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
>   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?
>Haskell mailing list
>Haskell at haskell.org

Graham Klyne
For email:

More information about the Haskell mailing list