[Haskell-beginners] How do I perform things inside a Monad?

Brent Yorgey byorgey at seas.upenn.edu
Thu Feb 7 23:25:32 CET 2013


On Thu, Feb 07, 2013 at 11:07:57PM +0100, Martin Drautzburg wrote:
> Hello all,
> 
> In the ALSA Midi package, there is an example, where he first sets up client, 
> port, queue and stuff. All of these are IO actions, and there are a number of 
> nested "do"s. Then in the innermost "do" he has all the pieces together and 
> starts playing. In this case the actual "song" is coded in the innermost do.
> 
> My question is: how can I inject a song from outside. Of course I could make 
> it another parameter, which would have to travel through all the "do"s, but I 
> already have more than enough parameters.

Just make it a parameter. If it is in scope, you can use it in the
innermost 'do'.  I'm not sure what you mean by "travel through all the
"do"s".  You don't have to do anything special to make this happen.
Like so:


foo bar baz = do
  withFoo $ \foo -> do
    blub
    withOtherThing $ \thing -> do
      blah
      something bar baz

Notice how 'bar' and 'baz' are automatically in scope throughout the
whole thing, even the innermost do.

-Brent



More information about the Beginners mailing list