Applicative functor evaluation is not clear to me

  • A+
Category:Languages

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 several times and am starting to doubt if even the author understands what this piece of code is doing.

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

An applicative functor applies a function in some context to a value in some context to get some result in some context. I have spent a few hours studying this code block and have come up with a few explanations for how this expression is evaluated, and none of them are satisfactory. I understand that (5+3)+(5*100) is 508, but the problem is getting to this expression. Does anyone have a clear explanation for this piece of code?

 


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.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: