Understanding Proper Use

For a list, why right apply (*>)does it behave like repeating and adding a second argument to ntimes, where nis the length of the first argument?

ghci> [1,2,3] *> [4,5]
[4,5,4,5,4,5]
+4
source share
1 answer

The operator is *>defined by default as

xs *> ys = id <$ xs <*> ys

which, in turn, translates by default to

const id <$> xs <*> ys

That is, it replaces each element xswith idto get xs', and then calculates xs' <*> ys. []is an instance Monadwhere (=<<) = concatMap. One of the laws Applicativedescribes the relationship between instances Applicativeand Monad:

pure = return
fs <*> as = fs `ap` as = fs >>= \f -> as >>= \a -> f a

For lists, this

fs <*> as = [f a | f <- fs, a <- as]

, *> Monad.

, Applicative , newtype Control.Applicative:

newtype ZipList a = ZipList [a]
instance Applicative ZipList where
  pure = repeat
  (<*>) = zipWith ($) 
+7

All Articles