[Haskell-beginners] Applicative: how <*> really works

Yassine yassine912 at gmail.com
Fri Aug 4 20:29:16 UTC 2017


Here is the link to stackoverflow:
https://stackoverflow.com/questions/45514315/applicative-functor-and-partial-application-how-it-works


Sorry there is a mistake in the definition of fmap.
It is:
case parse p inp

and not:
case p inp


2017-08-04 22:10 GMT+02:00 sasa bogicevic <brutallesale at gmail.com>:
> Ok there was a constructor missing, maybe you should create a gist so we can help out
>
> instance Functor Parser where
>   fmap g (P p) = P  (\inp -> case p inp of
>                          [] -> []
>                          [(v, out)] -> [(g v, out)])
>
> {
>         name: Bogicevic Sasa
>         phone: +381606006200
> }
>
>
>
>> On Aug 4, 2017, at 22:02, Jeffrey Brown <jeffbrown.the at gmail.com> wrote:
>>
>> If you post there, you could put the link on this thread; I'd be interested.
>>
>> On Fri, Aug 4, 2017 at 3:04 PM, Yassine <yassine912 at gmail.com> wrote:
>> Ok, thanks for your answer
>>
>> 2017-08-04 20:04 GMT+02:00 David McBride <toad3k at gmail.com>:
>> > This is a bit complicated for this list.  You might have a bit more
>> > luck posting this to stackoverflow.com.
>> >
>> > On Thu, Aug 3, 2017 at 3:19 PM, Yassine <yassine912 at gmail.com> wrote:
>> >> Hi,
>> >>
>> >> I have a question about functor applicate.
>> >>
>> >> I know that:
>> >> pure (+1) <*> Just 2
>> >>
>> >>
>> >> produce: Just 3
>> >> because pure (+1) produce Just (+1) and then Just (+1) <*> Just 2
>> >> produce Just (2+1)
>> >>
>> >>
>> >> but in more complex case like:
>> >> newtype Parser a = P (String -> [(a,String)])
>> >>
>> >> parse :: Parser a -> String -> [(a,String)]
>> >> parse (P p) inp = p inp
>> >>
>> >>
>> >> item :: Parser Char
>> >> item = P (\inp -> case inp of
>> >>  []     -> []
>> >> (x:xs) -> [(x,xs)])
>> >>
>> >> instance Functor Parser where
>> >> fmap g p = P (\inp -> case p inp of
>> >> []              -> []
>> >>  [(v, out)]      -> [(g v, out)])
>> >>
>> >> instance Applicative Parser where
>> >> pure v = P (\inp -> [(v, inp)])
>> >> pg <*> px = P (\inp -> case parse pg inp of
>> >> []              -> []
>> >> [(g, out)]      -> parse (fmap g px) out)
>> >>
>> >>
>> >> When I do:
>> >> parse (pure (\x y -> (x,y)) <*> item <*> item) "abc"
>> >>
>> >> The answer is:
>> >> [(('a','b'),"c")]
>> >>
>> >> But I don't understand what exactly happens.
>> >> First:
>> >> pure (\x y -> (x,y)) => P (\inp -> [(\x y -> (x,y), inp)])
>> >>
>> >> Then:
>> >> P (\inp -> [(\x y -> (x,y), inp)]) <*> item => ???
>> >>
>> >> Can someone explain what's happens step by step please.
>> >>
>> >> Thank you.
>> >> _______________________________________________
>> >> Beginners mailing list
>> >> Beginners at haskell.org
>> >> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>> > _______________________________________________
>> > Beginners mailing list
>> > Beginners at haskell.org
>> > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>> _______________________________________________
>> Beginners mailing list
>> Beginners at haskell.org
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>>
>>
>>
>> --
>> Jeff Brown | Jeffrey Benjamin Brown
>> Website   |   Facebook   |   LinkedIn(spammy, so I often miss messages here)   |   Github
>> _______________________________________________
>> Beginners mailing list
>> Beginners at haskell.org
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


More information about the Beginners mailing list