P
P
pythonMyLife2021-12-22 14:40:10
Haskell
pythonMyLife, 2021-12-22 14:40:10

Who are Alternative Functors?

I study Haskell, I understand who both Functors and Appilicative Functors are, but I found almost nothing in RuNet about Alternative Functors, I read the docks, however, with my level of English and the almost complete absence of examples, I'm holding back.

Can you please recommend any resources in Russian or examples with simple usage

PS: I re-read the docs, it says that this is a Monoid on an Applicative Functor. Well, I know very well what a monoid is.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Skusnov, 2021-12-22
@pythonMyLife

If you dance from the monoid union operation (<>), then two newtypes Sum and Product are made for numbers, that is, the sum and the product. For the monads of the Monad list, this product is
[(a,b) | a <- [1,2], b <- [3,4] ] -- will be [(1,3),(1,4),(2,3),(2,4)] (the list generator is syntactic sugar for a monad)
i.e. enumeration of each with each, and for MonadPlus this is addition (connection)
[1,2] <|> [3,4] -- will be [1,2,3,4]
Alternative is the same as MonadPlus, just MonadPlus appeared earlier.
By the way, Applicative (from which monads come) also has a product operator:
(,) <$> [1,2] <*> [3,4] -- will be [(1,3),(1,4),(2 ,3),(2,4)]
Alternative for Maybe implements newtype First, i.e. the first "non-null" element (if any).
Used in parsers. For example, if you want a string to start with a letter or a number, run two parsers with <|>.
You can look at Stepika, 2nd part of Haskell, lessons about parsers (lesson 1) and Alternative / MonadPlus ( lesson 2.5 )

M
Mikhail Potanin, 2022-03-03
@potan

The easiest way is to consider that a simple functor can raise a function from one argument, and applicative ones - from an arbitrary one.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question