haskell - 尝试遍历森林 Haskell 编译错误

我尝试为森林的每个节点分配一个数字,这样就不会有 2 个节点具有相同的数字。我尝试使用 2 个相互调用递归的函数,但我得到了一些编译错误。
这是代码:

numberTree :: Int -> Tree a -> (Tree Int,Int)

numberTree n (Node c []) = (Node n [], n+1)
numberTree n (Node _ (x:xs)) =  (Node n  fst.numberForest  (n+1) xs, snd.numberForest   (n+1) xs)


numberForest :: Int -> [Tree a] -> ([Tree Int],Int)

numberForest n (x:xs) = ((fst(numberTree n x )):(fst(numberForest (n+1) xs)), snd(numberForest (n+1) xs)+1)

numberForest n (x:xs) = ((fst(numberTree n x )):(fst(numberForest (n+1) xs)), snd(numberForest (n+1) xs)+1)

我得到的错误是:

.hs:27:34: Couldn't match expected type b0 -> c0' with actual typeTree Int Possible cause: Node' is applied to too many arguments In the first argument of(.)', namely `Node n fst' In the expression: Node n fst . numberForest (n + 1) xs


.hs:27:34:
Couldn't match expected type `Tree Int' with actual type `a0 -> c0
In the expression: Node n fst . numberForest (n + 1) xs
In the expression:
  (Node n fst . numberForest (n + 1) xs,
   snd . numberForest (n + 1) xs)
In an equation for `numberTree':
    numberTree n (Node _ (x : xs))
      = (Node n fst . numberForest (n + 1) xs,
         snd . numberForest (n + 1) xs)

 .hs:27:42:
Couldn't match type `(a1, b1) -> a1' with `[Tree Int]'
Expected type: Forest Int
  Actual type: (a1, b1) -> a1
Probable cause: `fst' is applied to too few arguments
In the second argument of `Node', namely `fst'
In the first argument of `(.)', namely `Node n fst'

 .hs:27:46:
Couldn't match expected type `a0 -> b0'
            with actual type `([Tree Int], Int)'
Possible cause: `numberForest' is applied to too many arguments
In the second argument of `(.)', namely `numberForest (n + 1) xs'
In the expression: Node n fst . numberForest (n + 1) xs

.hs:27:70:
Couldn't match expected type `Int' with actual type `a2 -> c1'
In the expression: snd . numberForest (n + 1) xs
In the expression:
  (Node n fst . numberForest (n + 1) xs,
   snd . numberForest (n + 1) xs)
In an equation for `numberTree':
    numberTree n (Node _ (x : xs))
      = (Node n fst . numberForest (n + 1) xs,
         snd . numberForest (n + 1) xs)

.hs:27:74:
Couldn't match expected type `a2 -> (a3, c1)'
            with actual type `([Tree Int], Int)'
Possible cause: `numberForest' is applied to too many arguments
In the second argument of `(.)', namely `numberForest (n + 1) xs'
In the expression: snd . numberForest (n + 1) xs

出了什么问题,我应该如何解决这个问题?

最佳答案

这条线

numberTree n (Node _ (x:xs)) =  (Node n  fst.numberForest  (n+1) xs, snd.numberForest   (n+1) xs)

实际上意味着
numberTree n (Node _ (x:xs)) =  ( (Node n fst) . (numberForest (n+1) xs)
                                , snd . (numberForest (n+1) xs))

它试图组合树而不是函数,导致编译器提示。你可能想要这样的东西:
numberTree n (Node _ (x:xs)) =  ( Node n  (fst (numberForest  (n+1) xs))
                                , snd (numberForest (n+1) xs))

但是,请注意上面的代码正在计算 numberForest (n+1) xs两次,导致指数级爆炸。您可以避免这种情况,例如使用 let ... in ...如下
numberTree n (Node _ (x:xs)) =  let result = numberForest (n+1) xs
                                in (Node n (fst result), snd result)

您可以使用 let 中的模式匹配进一步改进这一点。 :
numberTree n (Node _ (x:xs)) =  let (forest, n') = numberForest (n+1) xs
                                in (Node n forest, n')

https://stackoverflow.com/questions/27425200/

相关文章:

c++ - Visual Studio中D8049错误的原因是什么?

c# - 分配的左侧必须是变量属性或索引器

android - Android : Take numbers, do calculations,

compiler-errors - 编程新手,无法让Visual Studio 2013打开包含文件

oracle - 错误(3,31): PL/SQL: ORA-00984: Column is no

python - 无法运行Python脚本

c# - 具有只读哈希表的嵌套内部类在分配时引发Null ref异常。

python - 在 Python 中不使用\n 的新行

c# - C#简单兴趣计算器切换错误

c# - 错误: [feature] requires a Business (or higher)