[Haskell-beginners] audio generation

Daniel Bergey bergey at alum.mit.edu
Sun May 1 02:08:50 UTC 2016


On 2016-04-30 at 20:16, Dennis Raddle <dennis.raddle at gmail.com> wrote:
>     main :: IO ()
>     main = traverse_ print $ map sin xs
>
> Thanks. I'll see if this works for me. My question right now is, what is traverse_print?
> Is that the same as
>
> main = traverse print . map sin $ xs
>
> ?
>
> I'm guessing IO is traversable and for some reason you don't want to use mapM.

traverse_ is in Data.Foldable [1]

You're right that it's closely related to `traverse` and `mapM`.  I
generally prefer `traverse` and `traverse_` to `mapM` and `mapM_`
because they only require Applicative, not Monad.  So they work in more
cases, and generic code can be more generic.

The versions with the _ give back `f ()` instead of `f b` - in this
case, we get `IO ()` instead of `IO [()]`.  If you try with `traverse,
the program won't typecheck, because main needs to have type `IO ()`.

bergey

Footnotes: 
[1]  http://hackage.haskell.org/package/base-4.8.2.0/docs/Data-Foldable.html#v:traverse_



More information about the Beginners mailing list