[Haskell-cafe] Foldable, Traversable and choosing an order

Tom Ellis tom-lists-haskell-cafe-2017 at jaguarpaw.co.uk
Wed Sep 25 18:06:54 UTC 2019


On Wed, Sep 25, 2019 at 06:03:20PM +0100, Juan Casanova wrote:
> why isn't traverse just defined as
> 
> traverse_alt :: (Traversable t, Applicative f) => t (f a) -> f (t a)
> traverse_alt = traverse id
> 
> and let fmap deal with the mapping of the function? Of course this wouldn't
> be the implementation, it would be the other way around. Instances of
> Traversable would implement traverse_alt, and then whenever I wanted to do
> what traverse currently does, I would just do: traverse_alt (fmap f inputs).
> What is there to gain by including the mapping into the traversal *in the
> implementation of traverse itself*?

Your `traverse_alt` is called `sequence`:

    https://hackage.haskell.org/package/base-4.12.0.0/docs/src/Data.Traversable.html#Traversable

They're both useful and you can define each in terms of the other.  I'd say
I use traverse about ten times more than sequence.  It's basically
equivalent to `mapM`, i.e. "map with an effect".  `fmap` followed by
`sequence` tends to read a bit clumsily.

Tom


More information about the Haskell-Cafe mailing list