小编vij*_*icv的帖子

(fmap.fmap) func 与 fmap (fmap func) 的返回类型

以下是我正在测试的示例代码

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)

haskell

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

如何指定 Haskell 中代数类型的参数类型

如果我有如下所示的代数数据类型

\n
data BinaryTree a = Leaf \n                    | Node a (BinaryTree a ) (BinaryTree a)\n                    deriving (Eq,Ord)\n
Run Code Online (Sandbox Code Playgroud)\n

这里Leaf代表空,a是节点,另外两个参数是来自该节点的子树。

\n

有没有一种方法可以指定参数 a 应该导出Show

\n

我试图给出我自己的 Show for 实现BinaryTree,我一开始很简单,例如:

\n
instance Show (BinaryTree a) where\n    show Leaf = "x"\n    show (Node node left right) =  show node++ "\\n" ++ show left ++"  "++show right\n
Run Code Online (Sandbox Code Playgroud)\n

show node不起作用->No instance for (Show a) arising from a use of \xe2\x80\x98show\xe2\x80\x99

\n

haskell

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

标签 统计

haskell ×2