处理给定SuffixTree作为输入的函数,输出该后缀树中的整数列表。例如。 getIndices tree1 = [2,4,1,3,5,0]。整数列表的顺序无关紧要。我收到错误,在函数的第二行:“Couldn't match expected type 'SuffixTree' with actual type '[SuffixTree]'
”。我已经考虑了很久了,没有运气。任何帮助将不胜感激。
data SuffixTree = Leaf Int | Node [ ( String, SuffixTree ) ]
deriving (Eq,Ord,Show)
text1 :: String
text1 = "banana"
tree1 :: SuffixTree
tree1 = Node [("banana",Leaf 0),
("a",Node [("",Leaf 5),
("na",Node [("",Leaf 3),
("na",Leaf 1)])]),
("na",Node [("",Leaf 4),
("na",Leaf 2)])]
------------------------------------------------------------------
getIndices :: SuffixTree -> [ Int ]
getIndices sufTree = getIndices' sufTree []
where getIndices' :: SuffixTree -> [Int] -> [Int]
getIndices' (Node ((_, Node xs):ys)) c
| Node xs == Node [] = c
| otherwise = getIndices' ((Node xs):([Node ys])) c
getIndices' (Node ((_,Leaf i):xs)) c = getIndices' (Node xs) (i:c)
答案 0 :(得分:2)
您的getIndices'
实用程序函数声明为SuffixTree
,但在otherwise
情况下,您传递(Node xs:[Node ys])
类型为[SuffixTree]
的{{1}}。
鉴于你要做的就是收集树中的整数,或许你的otherwise
案例只需要调用getIndices'
两次:
| otherwise = getIndices' (Node xs) (getIndices' (Node ys) c)
您的代码还有其他一些问题。如果使用警告(-Wall
)进行编译,编译器将警告您模式匹配不完整。您的代码也因运行而在运行时失败。
不完整是因为getIndices'
未涵盖所有可能的SuffixTree
种类。您还需要填写getIndices' (Leaf Int)
和getIndices' (Node [])
的案例。
此外,| Node xs == Node []
案例中Node ((_, Node xs):ys
的现有案例会变得多余:它将通过getIndices'
案例中对otherwise
的递归调用来处理,那么新的Node []
案例。您也可以考虑如何将已经存在的两种情况简化为单个案例。