使用TimeUnit,如何将665477纳秒转换为0.665477毫秒?
long t = TimeUnit.MILLISECONDS.convert(665477L, TimeUnit.NANOSECONDS);
Run Code Online (Sandbox Code Playgroud)
这总是给出,0但我需要小数点精度.
我有以下代码:
data Tree a = ATree a [Tree a]
deriving Show
treeFold :: (b -> a -> b) -> b -> Tree a -> b
treeFold f acc (ATree a []) = f acc a
treeFold f acc (ATree a m) = foldl (treeFold f acc) (f acc a) m
Run Code Online (Sandbox Code Playgroud)
它应该遍历Tree的每个元素并将一个函数应用于该值.但它给了我这个错误:
Couldn't match type `Tree a' with `Tree a -> b'
Expected type: (Tree a -> b) -> a -> Tree a -> b
Actual type: b -> a -> …Run Code Online (Sandbox Code Playgroud) 为什么这会给出rigid type variable错误:
data MyTree a = Leaf [a]
| Branch (String, a) [MyTree a]
deriving (Show)
list :: MyTree a -> [a]
list (Leaf []) = []
list (Leaf m) = m
list (Branch _ (x:xs)) = list x ++ map (list) xs
-------------------------------------------------------------
Couldn't match type `a' with `[a]'
`a' is a rigid type variable bound by
the type signature for list :: MyTree a -> [a]
at test.hs:6:15
Expected type: MyTree a -> a
Actual …Run Code Online (Sandbox Code Playgroud) 我有这样的Tree数据结构:
data Tree a = ATree a [Tree a]
deriving Show
Run Code Online (Sandbox Code Playgroud)
是否有可能traverse使用以下声明编写一个更高阶的函数,该声明只是遍历(或者不管)树并重构它?
traverse :: (a -> b) -> Tree a -> Tree b
Run Code Online (Sandbox Code Playgroud)
请注意traverse签名.它没有得到树的列表.它一次只接受一个节点.
我认为签名需要更改为接受列表才能执行此操作.像这样:
traverse :: (a -> b) -> [Tree a] -> [Tree b]
Run Code Online (Sandbox Code Playgroud)