Implementing Parser Functor

后端 未结 2 351
别那么骄傲
别那么骄傲 2021-01-19 08:56

Working on Brent Yorgey\'s 2013 UPenn class homework, the following newtype exists:

newtype Parser a = Parser { runParser :: String -> Maybe (a

2条回答
  •  心在旅途
    2021-01-19 09:53

    To apply g on a Parser f, i.e. composite g and f. However, as g is a general function,and f returns Maybe(a,String), we need to convert g to

    Maybe(a,String)->d
    

    . Given first::(a->b)->(a,c)->(b,c) ,then

    first.Maybe :: Maybe(a->b,a->b)->Maybe(a,String)->Maybe(b,String)
    first.Maybe :: (a->b)->Maybe(a,String)->Maybe(b,String)
    first.Maybe g ::  Maybe(a,String)->Maybe(b,String)
    

    so

    instance Functor Parser where
      fmap g f = Parser $ fmap (first.Maybe g) . f
    

提交回复
热议问题