What monad is used with = << in this function?

First of all, the 1-Haskell-a-Day exercise confuses me to the point of no return. The goal is to filter list items if this does not match the following. For instance.

> filterByPair [1, 2, 2, 2, 3, 3, 4]
[2,2,3]

(I tried to make two lists of offsets, fastened them into tuples and deleted tuples that did not have the same number both times, for example [(2,2), (2,2), (3,3)], etc. .) But a reasonable simple solution uses the binding operator =<<:

filterByPair :: Eq a => [a] -> [a]
filterByPair = (init =<<) . group

I am having trouble choosing this operator. I do not get the same results in ghci when I try to use it:

> init =<< [2,3,4]
No instance for (Num [b0]) arising from the literal `2'

I find that in ghci I can use, say, replicates =<<. It seems that feeding each list item into a function:

> replicate 2 =<< [2,3,4]
[2,2,3,3,4,4]

, :

> replicate 2 2
[2,2]

- , replicate, , , , fmap :

> fmap (replicate 2) [2,3,4]
[[2,2],[3,3],[4,4]]

, . -, ? filterByPair? Eq ?

, =<< ghci, ?

+4
1

-, filterPairs :

filterPairs xs = group xs >>= init

, .

xs    :: Eq a => [a]
group :: Eq a => [a] -> [[a]]
init  :: [a] -> [a]

(>>=) :: Monad m => m a -> (a -> m b) -> m b

(>>=) group xs :: [[s]], , m ~ [] a ~ [s] (>>=),

(>>=) :: [[a]] -> ([a] -> [s]) -> [s]

, Monad [] , concatMap ( !). filterPairs

filterPairs xs = concatMap init (group xs)

, , concatMap, init group, , filterPairs.

+4

Source: https://habr.com/ru/post/1535210/


All Articles