Applicative functor evaluation is not clear to me

前端 未结 4 485
不思量自难忘°
不思量自难忘° 2020-12-02 01:29

I am currently reading Learn You a Haskell for Great Good! and am stumbling on the explanation for the evaluation of a certain code block. I\'ve read the explanations severa

4条回答
  •  误落风尘
    2020-12-02 01:36

    It is using the applicative instance for functions. Your code

    (+) <$> (+3) <*> (*100) $ 5
    

    is evaluated as

    ( (\a->b->a+b) <$> (\c->c+3) <*> (\d->d*100) ) 5
    ( (\x -> (\a->b->a+b) ((\c->c+3) x)) <*> (\d->d*100) ) 5
    ( (\x -> (\a->b->a+b) (x+3)) <*> (\d->d*100) ) 5
    ( (\x -> b -> (x+3)+b) <*> (\d->d*100) ) 5
    ( (\x->b->(x+3)+b) <*> (\d->d*100) ) 5
    (\y -> ((\x->b->(x+3)+b) y) ((\d->d*100) y)) 5
    (\y -> (b->(y+3)+b) (y*100)) 5
    (\y -> (y+3)+(y*100)) 5
    (5+3)+(5*100)
    

    where <$> is fmap or just function composition ., and <*> is ap if you know how it behaves on monads.

提交回复
热议问题