Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        [email protected]

You can reach the person managing the list at
        [email protected]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1. Re:  return IO () vs. () (Neil Jensen)
   2.  Handling a MonadError (Christopher Howard)
   3. Re:  Handling a MonadError (Christopher Done)


----------------------------------------------------------------------

Message: 1
Date: Sun, 29 May 2011 08:45:07 -0700
From: Neil Jensen <[email protected]>
Subject: Re: [Haskell-beginners] return IO () vs. ()
To: Chadda? Fouch? <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

On Sun, May 29, 2011 at 1:29 AM, Chadda? Fouch? <[email protected]>wrote:

> On Sun, May 29, 2011 at 3:33 AM, Antoine Latter <[email protected]>
> wrote:
> > I think you might have your parenthesis in the wrong spot.
> >
> > Instead of
> >
> >> x >>= (\a -> y a) >> z
> >
> > you probably want:
> >
> >> x >>= (\a -> y a >> z)
> >
> > Does that make sense?
> >
>
> No, that shouldn't change anything (except maybe performance, but not
> here), monads have a property close to associativity which means that
> changing the parenthesis shouldn't change the meaning of your code,
> you don't have to worry about that.
>
> --
> Jeda?
>

That's right Chadda?, I was just able to do the following and it worked:

calcCUVs account >>=  (\cuvs -> saveCUVs account cuvs)

Thanks.
N.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20110529/269404d8/attachment.html>

------------------------------

Message: 2
Date: Sun, 29 May 2011 19:05:15 -0800
From: Christopher Howard <[email protected]>
Subject: [Haskell-beginners] Handling a MonadError
To: Haskell Beginners <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Hi. I'm trying to learn Data.ConfigFile. I want to use the function
readfile:

[DESCRIPTION]
readfile :: MonadError CPError m => ConfigParser -> FilePath -> IO (m
ConfigParser)

Loads data from the specified file. It is then combined with the given
ConfigParser using the semantics documented under merge with the new
data taking precedence over the old. However, unlike merge, all the
options as set in the old object are preserved since the on-disk
representation does not convey those options.

May return an error if there is a syntax error. May raise an exception
if the file could not be accessed.
[/DESCRIPTION]

I understand that this function returns a monad, and I pull the value
out of it with (<-). And I believe this is similar to a "Maybe"
situation where I can do one thing if it provides a syntax error and
another thing if it provides the ConfigParser. But I'm not sure what
this would look like in actual code. Could somebody give me a small,
simple example?

-- 
frigidcode.com
theologia.indicium.us



------------------------------

Message: 3
Date: Mon, 30 May 2011 08:49:51 +0200
From: Christopher Done <[email protected]>
Subject: Re: [Haskell-beginners] Handling a MonadError
To: Christopher Howard <[email protected]>
Cc: Haskell Beginners <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"

On 30 May 2011 05:05, Christopher Howard
<[email protected]>wrote:

> Hi. I'm trying to learn Data.ConfigFile. I want to use the function
> readfile:
>
> [DESCRIPTION]
> readfile :: MonadError CPError m => ConfigParser -> FilePath -> IO (m
> ConfigParser)
>
> I understand that this function returns a monad, and I pull the value
> out of it with (<-). And I believe this is similar to a "Maybe"
> situation where I can do one thing if it provides a syntax error and
> another thing if it provides the ConfigParser. But I'm not sure what
> this would look like in actual code. Could somebody give me a small,
> simple example?
>

It's understandable that actual usage isn't clear, it wasn't clear to me
either when I first encountered it. There are a few ways to use it. It
returns an instance of error Monad, so you can either force it into an
Either value:

?> do parser <- readfile emptyCP "../confy.conf"; return (get (forceEither
parser) "PATHS" "upload_path") :: IO (Either CPError String)
Right "uploads"

Or you can get it within the monad like everything else:

?> do parser <- readfile emptyCP "../confy.conf"; return (do config <-
parser; get config "PATHS" "upload_path") :: IO (Either CPError String)
Right "uploads"

Because it returns any MonadError instance, and IO is an instance of
MonadError, you can skip the double level of monadery:

?> let getUploads ::  IO (Either CPError String); getUploads = runErrorT $
do parser <- liftIO $ readfile emptyCP "../confy.conf"; config <- parser;
get config "PATHS" "upload_path" in getUploads
Right "uploads"

Or with join:

?> :t join
join :: (Monad m) => m (m a) -> m a
?> let getUploads ::  IO (Either CPError String); getUploads = runErrorT $
do cp <- join $ liftIO $ readfile emptyCP "../confy.conf"; get cp "PATHS"
"upload_path" in getUploads
Right "uploads"

Control.Applicative also works nicely for this.

instance Applicative (ErrorT CPError IO) where (<*>) = ap; pure = return

?> let getCfg ::  IO (Either CPError (String,String));
       getCfg = runErrorT $ do
         cp <- join $ liftIO $ readfile emptyCP "../confy.conf";
         (,) <$> get cp "PATHS" "upload_path"
             <*> get cp "PATHS" "err_log"
   in getCfg
Right ("uploads","errors.log")
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20110530/1c0a4e2d/attachment-0001.htm>

------------------------------

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 35, Issue 59
*****************************************

Reply via email to