Send Beginners mailing list submissions to
        beginners@haskell.org

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
        beginners-requ...@haskell.org

You can reach the person managing the list at
        beginners-ow...@haskell.org

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


Today's Topics:

   1.  Folding an applicative functor, wait,.. what? (Arlen Cuss)
   2. Re:  Folding an applicative functor, wait,        .. what?
      (Felipe Almeida Lessa)
   3. Re:  Folding an applicative functor, wait,        .. what?
      (Patrick LeBoutillier)
   4. Re:  Folding an applicative functor, wait, .. what? (Arlen Cuss)
   5. Re:  Folding an applicative functor, wait, .. what? (Arlen Cuss)
   6. Re:  Folding an applicative functor, wait,        .. what?
      (Patrick LeBoutillier)
   7.  Is there a simpler way? Building a monad on      `IO' Monad
      (Arlen Cuss)


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

Message: 1
Date: Sat, 08 Jan 2011 23:45:17 +1100
From: Arlen Cuss <cel...@sairyx.org>
Subject: [Haskell-beginners] Folding an applicative functor, wait,..
        what?
To: beginners@haskell.org
Message-ID: <1294490717.11399.11.ca...@asu>
Content-Type: text/plain; charset="utf-8"

Hi all,

I'm probably in serious trouble here (i.e. I'm toying with things I
don't understand); I have some code that I was using like follows,
context removed for clarity:

anotherFunc :: Int -> String
...
map ((++) <$> show <*> anotherFunc) [1..20]

This works great - the integers 1..20 are passed to each function and
the results are catenated together.

What if I'd like to add a third function? So far, the only workable
solution I've been able to come up with is this:

map ((++) <$> ((++) <$> show <*> anotherFunc) <*> yetAnotherFunc)
[1..20]

That's one unfortunate expression. I've been messing around with concat
(trying to get the results of the functions into a list when all is well
and done for concat to finally string together?), but all in all, have
not had much luck with seeding the values in that way.

I think there is probably an obvious solution I've missed... and hold on
a minute!

map (mconcat [show, anotherFunc, yetAnotherFunc]) [1..20]

I just stumbled upon this. While I'm not 100% confident on what's just
happened here, I thought I'd mail this out for everyone to enjoy the
learning process that has been this email.

This serves as a helpful pointer for me to study both monoids and
applicative functors in depth, as the former suddenly leapt out of the
recesses of my mind somewhere and solved it.

Thanks for listening, and nice to meet you all!

Cheers,
Arlen
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: This is a digitally signed message part
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20110108/481e9d84/attachment-0001.pgp>

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

Message: 2
Date: Sat, 8 Jan 2011 10:53:00 -0200
From: Felipe Almeida Lessa <felipe.le...@gmail.com>
Subject: Re: [Haskell-beginners] Folding an applicative functor, wait,
        .. what?
To: Arlen Cuss <cel...@sairyx.org>
Cc: beginners@haskell.org
Message-ID:
        <aanlkti=r1qzqyd3ri-creu6d69e++rxoijgnztusf...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

On Sat, Jan 8, 2011 at 10:45 AM, Arlen Cuss <cel...@sairyx.org> wrote:
> I think there is probably an obvious solution I've missed... and hold on
> a minute!
>
> map (mconcat [show, anotherFunc, yetAnotherFunc]) [1..20]
>
> I just stumbled upon this. While I'm not 100% confident on what's just
> happened here, I thought I'd mail this out for everyone to enjoy the
> learning process that has been this email.

That's the Monoid b => Monoid (a->b) instance [1], but I don't think
it is anywhere near obvious =).

Cheers!

[1] 
http://hackage.haskell.org/packages/archive/base/4.3.0.0/doc/html/src/Data-Monoid.html#(line94)

-- 
Felipe.



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

Message: 3
Date: Sat, 8 Jan 2011 12:58:03 -0500
From: Patrick LeBoutillier <patrick.leboutill...@gmail.com>
Subject: Re: [Haskell-beginners] Folding an applicative functor, wait,
        .. what?
To: Arlen Cuss <cel...@sairyx.org>
Cc: beginners@haskell.org
Message-ID:
        <aanlktimym9bsvgpvgcnbekam+wuurqgrutd1osp5l...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1

Hi,

Initially I didn't even understand how your code worked, but I found a
small tutorial un this kind of stuff here:

http://learnyouahaskell.com/functors-applicative-functors-and-monoids#applicative-functors

Just search for the phrase "Another instance of Applicative is (->) r,
so functions" and you will see examples similar to yours.

I can't wait for the "Learn You a Haskell" Book!


Patrick


On Sat, Jan 8, 2011 at 7:45 AM, Arlen Cuss <cel...@sairyx.org> wrote:
> Hi all,
>
> I'm probably in serious trouble here (i.e. I'm toying with things I
> don't understand); I have some code that I was using like follows,
> context removed for clarity:
>
> anotherFunc :: Int -> String
> ...
> map ((++) <$> show <*> anotherFunc) [1..20]
>
> This works great - the integers 1..20 are passed to each function and
> the results are catenated together.
>
> What if I'd like to add a third function? So far, the only workable
> solution I've been able to come up with is this:
>
> map ((++) <$> ((++) <$> show <*> anotherFunc) <*> yetAnotherFunc)
> [1..20]
>
> That's one unfortunate expression. I've been messing around with concat
> (trying to get the results of the functions into a list when all is well
> and done for concat to finally string together?), but all in all, have
> not had much luck with seeding the values in that way.
>
> I think there is probably an obvious solution I've missed... and hold on
> a minute!
>
> map (mconcat [show, anotherFunc, yetAnotherFunc]) [1..20]
>
> I just stumbled upon this. While I'm not 100% confident on what's just
> happened here, I thought I'd mail this out for everyone to enjoy the
> learning process that has been this email.
>
> This serves as a helpful pointer for me to study both monoids and
> applicative functors in depth, as the former suddenly leapt out of the
> recesses of my mind somewhere and solved it.
>
> Thanks for listening, and nice to meet you all!
>
> Cheers,
> Arlen
>
> _______________________________________________
> Beginners mailing list
> Beginners@haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
>



-- 
=====================
Patrick LeBoutillier
Rosem?re, Qu?bec, Canada



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

Message: 4
Date: Sun, 09 Jan 2011 09:24:53 +1100
From: Arlen Cuss <cel...@sairyx.org>
Subject: Re: [Haskell-beginners] Folding an applicative functor, wait,
        .. what?
To: Patrick LeBoutillier <patrick.leboutill...@gmail.com>
Cc: beginners@haskell.org
Message-ID: <1294525493.2984.0.ca...@asu>
Content-Type: text/plain; charset="utf-8"

Hi,

> Initially I didn't even understand how your code worked, but I found a
> small tutorial un this kind of stuff here:
> 
> http://learnyouahaskell.com/functors-applicative-functors-and-monoids#applicative-functors
> 
> Just search for the phrase "Another instance of Applicative is (->) r,
> so functions" and you will see examples similar to yours.

This is actually where I've been learning Haskell from; these parts of
the book sort of half-sunk in, but now that I'm actually trying to use
them it's sinking in better ;-)

> I can't wait for the "Learn You a Haskell" Book!

Would be great!

> Patrick

Cheers,
Arlen

> On Sat, Jan 8, 2011 at 7:45 AM, Arlen Cuss <cel...@sairyx.org> wrote:
> > Hi all,
> >
> > I'm probably in serious trouble here (i.e. I'm toying with things I
> > don't understand); I have some code that I was using like follows,
> > context removed for clarity:
> >
> > anotherFunc :: Int -> String
> > ...
> > map ((++) <$> show <*> anotherFunc) [1..20]
> >
> > This works great - the integers 1..20 are passed to each function and
> > the results are catenated together.
> >
> > What if I'd like to add a third function? So far, the only workable
> > solution I've been able to come up with is this:
> >
> > map ((++) <$> ((++) <$> show <*> anotherFunc) <*> yetAnotherFunc)
> > [1..20]
> >
> > That's one unfortunate expression. I've been messing around with concat
> > (trying to get the results of the functions into a list when all is well
> > and done for concat to finally string together?), but all in all, have
> > not had much luck with seeding the values in that way.
> >
> > I think there is probably an obvious solution I've missed... and hold on
> > a minute!
> >
> > map (mconcat [show, anotherFunc, yetAnotherFunc]) [1..20]
> >
> > I just stumbled upon this. While I'm not 100% confident on what's just
> > happened here, I thought I'd mail this out for everyone to enjoy the
> > learning process that has been this email.
> >
> > This serves as a helpful pointer for me to study both monoids and
> > applicative functors in depth, as the former suddenly leapt out of the
> > recesses of my mind somewhere and solved it.
> >
> > Thanks for listening, and nice to meet you all!
> >
> > Cheers,
> > Arlen
> >
> > _______________________________________________
> > Beginners mailing list
> > Beginners@haskell.org
> > http://www.haskell.org/mailman/listinfo/beginners
> >
> >
> 
> 
> 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: This is a digitally signed message part
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20110109/9921af61/attachment-0001.pgp>

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

Message: 5
Date: Sun, 09 Jan 2011 09:25:45 +1100
From: Arlen Cuss <cel...@sairyx.org>
Subject: Re: [Haskell-beginners] Folding an applicative functor, wait,
        .. what?
To: Felipe Almeida Lessa <felipe.le...@gmail.com>
Cc: beginners@haskell.org
Message-ID: <1294525545.2984.1.ca...@asu>
Content-Type: text/plain; charset="utf-8"

On Sat, 2011-01-08 at 10:53 -0200, Felipe Almeida Lessa wrote:
> On Sat, Jan 8, 2011 at 10:45 AM, Arlen Cuss <cel...@sairyx.org> wrote:
> > I think there is probably an obvious solution I've missed... and hold on
> > a minute!
> >
> > map (mconcat [show, anotherFunc, yetAnotherFunc]) [1..20]
> >
> > I just stumbled upon this. While I'm not 100% confident on what's just
> > happened here, I thought I'd mail this out for everyone to enjoy the
> > learning process that has been this email.
> 
> That's the Monoid b => Monoid (a->b) instance [1], but I don't think
> it is anywhere near obvious =).

Right you are! I know what's going on now :-) Cheers!

> Cheers!
> 
> [1] 
> http://hackage.haskell.org/packages/archive/base/4.3.0.0/doc/html/src/Data-Monoid.html#(line94)

Arlen


-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: This is a digitally signed message part
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20110109/59e64302/attachment-0001.pgp>

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

Message: 6
Date: Sat, 8 Jan 2011 19:40:43 -0500
From: Patrick LeBoutillier <patrick.leboutill...@gmail.com>
Subject: Re: [Haskell-beginners] Folding an applicative functor, wait,
        .. what?
To: Arlen Cuss <cel...@sairyx.org>
Cc: beginners@haskell.org
Message-ID:
        <aanlktimpkhytf-5ce5txkjore8quwjskxu7v=nfct...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1

Arlen,

>> I can't wait for the "Learn You a Haskell" Book!
>
> Would be great!

Look here, it's coming:

http://www.amazon.com/Learn-You-Haskell-Great-Good/dp/1593272839/ref=sr_1_1?ie=UTF8&s=books&qid=1294533476&sr=8-1

-- 
=====================
Patrick LeBoutillier
Rosem?re, Qu?bec, Canada



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

Message: 7
Date: Sun, 09 Jan 2011 21:47:40 +1100
From: Arlen Cuss <cel...@sairyx.org>
Subject: [Haskell-beginners] Is there a simpler way? Building a monad
        on      `IO' Monad
To: beginners@haskell.org
Message-ID: <1294570060.15329.6.ca...@asu>
Content-Type: text/plain; charset="utf-8"

Hi all,

Thanks for previous help on this list! I really appreciate it.

Today I wrote a monad and I'm not sure if I took a complicated way about
it. It's essentially a State monad, except with some specialised
functions that operate on the `state' if you will (though the state is
rarely mutated)?you initialise it with two Handles (e.g. stdin, stdout),
and then a set of specialised functions defined `within' the monad will
operate on those handles. It saves you from passing the Handles
throughout the functions and subcalls.

It's quite possible there already exists a monad for this job, or that
IO will actually let you do this, but I didn't find it in a bit of
searching, and concluded this would be a fun way to solve the problem.
If anyone has any advice on shortening the code, or possibly removing
the need for it, please let me know!

Here's the main monad:

> import System.IO
> import Control.Applicative
> 
> newtype IODirector a = IODirector { runIODirector :: (Handle,Handle)
-> IO (a, (Handle,Handle)) }
> 
> instance Monad IODirector where
>   return a = IODirector $ \hs -> return (a, hs)
>   m >>= k  = IODirector $ \hs -> do (a, hs) <- runIODirector m hs
>                                     runIODirector (k a) hs

This is basically the same as State, except we `return' to the IO monad,
as is the result of the stateful computation an I/O action, IO (a,
(Handle,Handle)).

We then have a type-class for the actual I/O we can perform within the
monad:

> class MonadDirectedIO a where
>   dPutStr :: String -> a ()
>   dPutStrLn :: String -> a ()
> 
>   dGetLine :: a String
>   dGetChar :: a Char
...

The functions here continue for all the ones from IO I really wanted to
use, and the instance is not surprising:

> instance MonadDirectedIO IODirector where
>   dPutStr s = IODirector $ \hs@(_,hOut) -> do hPutStr hOut s
>                                               return ((), hs)
>   dPutStrLn = dPutStr . (++ "\n")
> 
>   dGetLine = IODirector $ \hs@(hIn,_) -> do r <- hGetLine hIn
>                                             return (r, hs)
>   dGetChar = IODirector $ \hs@(hIn,_) -> do r <- hGetChar hIn
>                                             return (r, hs)

I'm aware I didn't have to put this in a type-class, but it seemed a
reasonable thing to do.

There was a little plumbing work to `enclose' IO within this monad. My
question is - did I do it right? Or is there a simpler way?

Of course, if there's already such functionality in the built-in, I'd
also be interested to hear ... ;-)

Cheers!

Arlen
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: This is a digitally signed message part
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20110109/94c42b4a/attachment-0001.pgp>

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

_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 31, Issue 6
****************************************

Reply via email to