小编Dap*_* Li的帖子

fmap一个2-arity函数Nothing应该返回Nothing?

从源头MaybeGHC:

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)

haskell ghci

1
推荐指数
1
解决办法
95
查看次数

标签 统计

ghci ×1

haskell ×1