以下是我正在测试的示例代码
import Control.Exception
safeLoad :: FilePath -> IO (Either IOException String)
safeLoad f = (Right <$> readFile f) `catch` (pure . Left)
fileChars :: FilePath -> IO (Either IOException Int)
fileChars = fmap (fmap length) . safeLoad
fileChars' :: FilePath -> IO Int
fileChars' = (fmap.fmap) length safeLoad
example :: [Maybe Integer]
example = (fmap.fmap) (+1) [Just 2,Just 3]
Run Code Online (Sandbox Code Playgroud)
这里的类型(fmap.fmap)评估为:
ghci> :t ((fmap.fmap) length)
((fmap.fmap) length)
:: (Functor f1, Functor f2, Foldable t) =>
f1 (f2 (t a)) …Run Code Online (Sandbox Code Playgroud) 如果我有如下所示的代数数据类型
\ndata BinaryTree a = Leaf \n | Node a (BinaryTree a ) (BinaryTree a)\n deriving (Eq,Ord)\nRun Code Online (Sandbox Code Playgroud)\n这里Leaf代表空,a是节点,另外两个参数是来自该节点的子树。
\n有没有一种方法可以指定参数 a 应该导出Show
我试图给出我自己的 Show for 实现BinaryTree,我一开始很简单,例如:
instance Show (BinaryTree a) where\n show Leaf = "x"\n show (Node node left right) = show node++ "\\n" ++ show left ++" "++show right\nRun Code Online (Sandbox Code Playgroud)\n但show node不起作用->No instance for (Show a) arising from a use of \xe2\x80\x98show\xe2\x80\x99
haskell ×2