[Haskell-beginners] Functor instance
Hilco Wijbenga
hilco.wijbenga at gmail.com
Sat Mar 3 20:32:34 UTC 2018
Hi all,
I'm trying to implement my own Result type (and yes, I'm aware you can
abuse Either for this :-) ) but doing something as (seemingly?) simple
as implementing a Functor instance was surprisingly difficult.
data Result failure success
= Success success
| Failure failure
instance Functor (Result failure) where
fmap f (Success value) = Success (f value)
fmap _ (Failure error) = Failure error
-- fmap _ result@(Failure error) = result
-- fmap _ result = result
1) Is it possible to define "Result" as "Result success failure"
(instead of "Result failure success") and _still_ create an instance
of Functor?
2) The two alternatives for fmap for the Failure scenario do not
compile (the end result is "Result failure a" instead of "Result
failure b") and that makes sense. But I would like to be able to
express that "result" is not touched. Is there any way to do that?
3) And while wondering about that, is GHC smart enough to realize that
"= Failure error" in the failure scenario is actually a NOP? (I'm just
curious.)
Cheers,
Hilco
More information about the Beginners
mailing list