从源头Maybe
在GHC:
instance Functor Maybe where
fmap _ Nothing = Nothing
fmap f (Just a) = Just (f a)
Run Code Online (Sandbox Code Playgroud)
什么时候fmap
应用Nothing
,它应该返回Nothing
.
例如,在ghci(v8.2.2)中运行它:
Prelude> fmap (+1) Nothing
Nothing
Run Code Online (Sandbox Code Playgroud)
但是,当我应用一个arity为2的函数时:
Prelude> fmap (++) Nothing
<interactive>:11:1: error:
• No instance for (Show ([a0] -> [a0]))
arising from a use of ‘print’
(maybe you haven't applied a function to enough arguments?)
• In a stmt of an interactive GHCi command: print it
Run Code Online (Sandbox Code Playgroud)
事实上,结果似乎是Nothing
:
Prelude> …
Run Code Online (Sandbox Code Playgroud)