Spin off of this question. Intuitively I have understood what sequenceA
does in that usecase, but not how/why it works like that.
So it all boils down to this question: how does sequenceA
work in the following case?
> sequenceA [("a",1),("b",2),("c",3)]
("abc",[1,2,3])
I see that
sequenceA :: (Traversable t, Applicative f) => t (f a) -> f (t a)
so in the usecase above the Traversable
is []
, and the Applicative
, since (,)
is a binary type constructor, is (,) a
, which means that the pair is taken as an applicative functor on its snd
field. And this goes together the list ending up in the snd
of the result. So we go from a list of pairs to a pair with a list in its second field.
But where does the "abc"
come from? I mean, I know that it's the concatenation of the fst
of all the pairs, but I don't know if it's via ++
or via concat
of the list of the fst
s. There seems to be nothing in sequenceA
's signature to enforce that the fst
s of the pairs can be combined together.
Still that assumption has to be used somewhere. Indeed, the following fails
sequenceA [('a',1),('b',2),('c',3)]