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: Please explain the "$ \foo ->do" idiom (Martin Drautzburg)
2. Re: Please explain the "$ \foo ->do" idiom (Stephen Tetley)
----------------------------------------------------------------------
Message: 1
Date: Sat, 9 Feb 2013 10:41:59 +0100
From: Martin Drautzburg <[email protected]>
Subject: Re: [Haskell-beginners] Please explain the "$ \foo ->do"
idiom
To: [email protected]
Message-ID: <[email protected]>
Content-Type: Text/Plain; charset="iso-8859-15"
On Friday, 8. February 2013 08:26:38 Henk-Jan van Tuyl wrote:
> On Wed, 06 Feb 2013 22:31:05 +0100, Martin Drautzburg
>
> <[email protected]> wrote:
> > Can
> > someone please walk me through it and possibly show ways to avoid the
> > massive
> > nesting.
> >
> > dtz = do
> >
> > SndSeq.withDefault SndSeq.Block $ \h -> do
> >
> > Client.setName (h :: SndSeq.T SndSeq.DuplexMode) "Haskell-Melody"
> > Port.withSimple h "out"
> >
> > (Port.caps [Port.capRead, Port.capSubsRead, Port.capWrite])
> > (Port.types [Port.typeMidiGeneric, Port.typeApplication]) $ \p
> >
> > -> do
> >
> > Queue.with h $ \q -> do
> >
> > c <- Client.getId h
> > let me = Addr.Cons c p
> > conn <- parseDestArgs h me ["20:0"]
> > Queue.control h q Event.QueueStart Nothing
> > Queue.control h q (Event.QueueTempo (Event.Tempo
> >
> > 10000000)) Nothing
> >
> > return ()
>
> I like to divide large functions into several smaller ones:
>
> dtz =
> SndSeq.withDefault SndSeq.Block f1
> where
> f1 h =
> do
> Client.setName (h :: SndSeq.T SndSeq.DuplexMode)
> "Haskell-Melody" Port.withSimple h "out"
> (Port.caps [Port.capRead, Port.capSubsRead, Port.capWrite])
> (Port.types [Port.typeMidiGeneric, Port.typeApplication]) (f2
> h)
> f2 h p = Queue.with h (f3 h p)
> f3 h p q =
> do
> c <- Client.getId h
> let me = Addr.Cons c p
> conn <- parseDestArgs h me ["20:0"]
> Queue.control h q Event.QueueStart Nothing
> Queue.control h q (Event.QueueTempo (Event.Tempo 10000000))
> Nothing
> return ()
I tried to do exactly this and I like it a bit better. But note how those
functions get quite a number of parameters. In the nested "do", evertything
was in scope.
Things get worse, when I try to run something in the innermost "do". When I
use individual functions, I need yet another parameter which needs to travel
through all these functions. As Brent Yorgey suggested, the nested "do"s can
accept another parameter in the topmost function and it will be automatically
in scope all the way down.
I really wish, someone could elaboreate on this "$ \foo ->do" pattern,
- when it is typically used,
- what determines the depth of the nesting,
- its pros and cons and the possible alternatives.
It seems to be some kind of idiom,
--
Martin
------------------------------
Message: 2
Date: Sat, 9 Feb 2013 10:45:27 +0000
From: Stephen Tetley <[email protected]>
Subject: Re: [Haskell-beginners] Please explain the "$ \foo ->do"
idiom
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Message-ID:
<cab2tprbajh_zofonygwnqscg4pfguspm09762nmpztpj7zm...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1
> I really wish, someone could elaborate on this "$ \foo ->do" pattern,
If you are using it in the IO monad consider it as "using a handle"
(cf. a file handle or network connection) - as you go outside the IO
monad you'll note that a "handle" is really quite a general thing.
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 56, Issue 17
*****************************************