尝试迭代林Haskell编译错误

尝试迭代林Haskell编译错误,haskell,compiler-errors,tree,parse-forest,Haskell,Compiler Errors,Tree,Parse Forest,我尝试为林的每个节点分配一个编号,这样就不会有两个节点具有相同的编号。我尝试使用两个函数,它们相互调用递归函数,但我得到一些编译错误。 代码如下: 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.number

我尝试为林的每个节点分配一个编号,这样就不会有两个节点具有相同的编号。我尝试使用两个函数,它们相互调用递归函数,但我得到一些编译错误。 代码如下:

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: 无法将预期类型
b0->c0'与实际类型
Tree Int匹配
可能原因:
Node'应用于太多参数
在
(.)的第一个参数中,即'Node n fst' 在表达式中:Node n fst。数字森林(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。。。在…
中,如下所示

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')
这条线

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。。。在…
中,如下所示

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')

州单子是你的朋友

import Control.Monad.Trans.State

data Tree   a = Node a (Forest a)
type Forest a = [Tree a]

numberTreeS :: Tree a -> State Int (Tree Int)
numberTreeS (Node _ xs) = do
    n <- get
    put (n + 1)
    xs' <- numberForestS xs
    return $ Node n xs'

numberForestS :: Forest a -> State Int (Forest Int)
numberForestS = mapM numberTreeS

numberForest :: Forest a -> Forest Int
numberForest xs = evalState (numberForestS xs) 0
import Control.Monad.Trans.State
数据树a=节点a(林a)
类型林a=[树a]
numberTreeS::树a->状态Int(树Int)
numberTreeS(节点xs)=do
n状态Int(森林Int)
numberForestS=mapM numberTreeS
numberForest::Forest a->Forest Int
numberforestxs=evalState(numberforestxs)0

这比显式状态传递更具可读性。

状态单子是您的朋友

import Control.Monad.Trans.State

data Tree   a = Node a (Forest a)
type Forest a = [Tree a]

numberTreeS :: Tree a -> State Int (Tree Int)
numberTreeS (Node _ xs) = do
    n <- get
    put (n + 1)
    xs' <- numberForestS xs
    return $ Node n xs'

numberForestS :: Forest a -> State Int (Forest Int)
numberForestS = mapM numberTreeS

numberForest :: Forest a -> Forest Int
numberForest xs = evalState (numberForestS xs) 0
import Control.Monad.Trans.State
数据树a=节点a(林a)
类型林a=[树a]
numberTreeS::树a->状态Int(树Int)
numberTreeS(节点xs)=do
n状态Int(森林Int)
numberForestS=mapM numberTreeS
numberForest::Forest a->Forest Int
numberforestxs=evalState(numberforestxs)0

这比显式状态传递更具可读性。

您应该包括类型
是如何定义的。
节点n fst。numberForest(n+1)xs
->也许这应该是
节点n$fst$numberForest(n+1)xs
?您应该包括类型
树的定义方式。
节点n fst。numberForest(n+1)xs
->也许这应该是
节点n$fst$numberForest(n+1)xs