[Haskell-beginners] Can someone help to unnest this "do" cascade

Henk-Jan van Tuyl hjgtuyl at chello.nl
Fri Feb 8 08:26:38 CET 2013


On Wed, 06 Feb 2013 22:31:05 +0100, Martin Drautzburg
<Martin.Drautzburg at web.de> 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 ()

f1, f2 and f3 might be replaced with more meaningful names. The "return
()" at the end can be removed; such things can be found with hlint[0].

Regards,
Henk-Jan van Tuyl


[0] http://hackage.haskell.org/package/hlint

-- 
http://Van.Tuyl.eu/
http://members.chello.nl/hjgtuyl/tourdemonad.html
Haskell programming
--



More information about the Beginners mailing list