Wednesday 15 September 2010

the definition of Applicative instance implementation for Maybe (Haskell/LYAH) -



the definition of Applicative instance implementation for Maybe (Haskell/LYAH) -

many times tried understand functor , monad in haskell, failed. time when reached @ lyah's applicative functor, , thought understood applicative typeclasses, had uncertainty applicative instance implementation maybe:

instance applicative maybe pure = nil <*> _ = nil (just f) <*> = fmap f

for lastly line of above snippet, understanding follows:

(1) just f appropriated f (a->b) in (<*>) :: f (a -> b) -> f -> f b of class definition of applicative, , f appropriated (a->b)

(2) something appropriated f a.

my question (2), why there not written just something or like?

in functor instance implementation maybe, there fmap f (just x) = (f x) , just x appropriated f a of fmap :: (a -> b) -> f -> f b.

for verifying understanding, changed something just something:

instance applicative maybe pure = nil <*> _ = nil (just f) <*> (just something) = fmap f (just something)

under gchi, tried

just (+3) <*> 9

and got same result just 12, when tried

just (++"hahah") <*> nothing`

i got error

*** exception: functor.hs:(88,3)-(89,60): non-exhaustive patterns in function <*>

i don't know why or things miss?

(this extension of comment , not reply question)

this alternate definition easier grasp:

instance applicative maybe pure = (just f) <*> (just x) = (f x) _ <*> _ = nil

i think fell trap of interpreting much out of name something. seek name variables f , x in context of applicatives (in context of <*>, precise), that's are, function , value function applied to.

haskell

No comments:

Post a Comment