Applicative functor in lists

I understand that the following will be:

[(+2),(+1)]<*>[1,2,3] == [3,4,5,2,3,4]

I also understand what fmapis being implemented as map. But how could I mentally contrast this calculation in my head? The first time I saw this, I suggested the following:

[(*2),(+1)]<*>[1,2,3] == [4,5,6]

The second time, though I'm talking about him [[3,4,5],[2,3,4]]. But on the other hand , it <*>returns f b, so I knew that it would be impossible. So my question is: what are the mental steps to understand this?

+4
source share
2 answers

fs <*> xsmore or less [f x | f <- fs, x <- xs]. Monads have an applicative instance

fM <*> xM = do 
  f <- fM
  x <- xM
  return (f x)

which directly corresponds to the concept of a list.

+12
source

, <*> × ( ):

[a, b, c] × [x, y, z] == [a x, a y, a z, b x, b y, ...]
+5

All Articles