-给出两个列表,一个列表按顺序排序,另一个列表按顺序排序。这两个列表来自同一棵二叉树。有了这两个列表,二叉树就被重建了。
-我还没有在线找到“排名”功能。我的教授告诉我们,“等级”功能可以输出列表中一个元素的位置。
在使用函数“ rank”的以下行中发生了错误。
所以我有两个问题。
main :: IO () -- This says that main is an IO action.
main = return () -- This tells main to do nothing
data IntTree = Empty | Branch IntTree Int IntTree deriving (Show, Eq)
reconstruct :: [Int]->IntTree
-- Pattern matching
reconstruct (x:xs) y = Branch (reconstruct take((rank x y) xs) take ((rank x y) y)) x x (reconstruct drop ((rank x y)+1 xs) drop ((rank x y)+1 y))
校正后
import Data.List
main :: IO () -- This says that main is an IO action.
main = return () -- This tells main to do nothing
data IntTree = Empty | Branch IntTree Int IntTree deriving (Show, Eq)
--Two lists are given, one sorted by preorder, the other sorted by inorder.
-- And the two lists are from the same binary tree. With the two lists the binary tree is reconstructed.
reconstruct :: [Int]->[Int]->IntTree
-- Pattern matching
reconstruct [] [] = Empty
reconstruct (x:xs) y = Branch (reconstruct take (length (fst p) xs) (fst p)) x (reconstruct drop (length (fst p) xs) (snd p))
where p = span (x/=) y
reconstruct _ _ = error "incomplete pattern"
错误
E:\Haskell\Uebungsblatt_2_Aufgabe_1.hs:15:32: error:
* Couldn't match expected type `[Int] -> IntTree'
with actual type `IntTree'
* The function `reconstruct' is applied to three arguments,
but its type `[Int] -> [Int] -> IntTree' has only two
In the first argument of `Branch', namely
`(reconstruct take (length (fst p) xs) (fst p))'
In the expression:
Branch
(reconstruct take (length (fst p) xs) (fst p))
x
(reconstruct drop (length (fst p) xs) (snd p))
|
15 | reconstruct (x:xs) y = Branch (reconstruct take (length (fst p) xs) (fst p)) x (reconstruct drop (length (fst p) xs) (snd p))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
E:\Haskell\Uebungsblatt_2_Aufgabe_1.hs:15:44: error:
* Couldn't match expected type `[Int]'
with actual type `Int -> [a0] -> [a0]'
* Probable cause: `take' is applied to too few arguments
In the first argument of `reconstruct', namely `take'
In the first argument of `Branch', namely
`(reconstruct take (length (fst p) xs) (fst p))'
In the expression:
Branch
(reconstruct take (length (fst p) xs) (fst p))
x
(reconstruct drop (length (fst p) xs) (snd p))
|
15 | reconstruct (x:xs) y = Branch (reconstruct take (length (fst p) xs) (fst p)) x (reconstruct drop (length (fst p) xs) (snd p))
| ^^^^
E:\Haskell\Uebungsblatt_2_Aufgabe_1.hs:15:50: error:
* Couldn't match expected type `[Int] -> [Int]'
with actual type `Int'
* The function `length' is applied to two arguments,
but its type `[Int] -> Int' has only one
In the second argument of `reconstruct', namely
`(length (fst p) xs)'
In the first argument of `Branch', namely
`(reconstruct take (length (fst p) xs) (fst p))'
|
15 | reconstruct (x:xs) y = Branch (reconstruct take (length (fst p) xs) (fst p)) x (reconstruct drop (length (fst p) xs) (snd p))
| ^^^^^^^^^^^^^^^^^
E:\Haskell\Uebungsblatt_2_Aufgabe_1.hs:15:81: error:
* Couldn't match expected type `[Int] -> IntTree'
with actual type `IntTree'
* The function `reconstruct' is applied to three arguments,
but its type `[Int] -> [Int] -> IntTree' has only two
In the third argument of `Branch', namely
`(reconstruct drop (length (fst p) xs) (snd p))'
In the expression:
Branch
(reconstruct take (length (fst p) xs) (fst p))
x
(reconstruct drop (length (fst p) xs) (snd p))
|
15 | reconstruct (x:xs) y = Branch (reconstruct take (length (fst p) xs) (fst p)) x (reconstruct drop (length (fst p) xs) (snd p))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
E:\Haskell\Uebungsblatt_2_Aufgabe_1.hs:15:93: error:
* Couldn't match expected type `[Int]'
with actual type `Int -> [a1] -> [a1]'
* Probable cause: `drop' is applied to too few arguments
In the first argument of `reconstruct', namely `drop'
In the third argument of `Branch', namely
`(reconstruct drop (length (fst p) xs) (snd p))'
In the expression:
Branch
(reconstruct take (length (fst p) xs) (fst p))
x
(reconstruct drop (length (fst p) xs) (snd p))
|
15 | reconstruct (x:xs) y = Branch (reconstruct take (length (fst p) xs) (fst p)) x (reconstruct drop (length (fst p) xs) (snd p))
| ^^^^
E:\Haskell\Uebungsblatt_2_Aufgabe_1.hs:15:99: error:
* Couldn't match expected type `[Int] -> [Int]'
with actual type `Int'
* The function `length' is applied to two arguments,
but its type `[Int] -> Int' has only one
In the second argument of `reconstruct', namely
`(length (fst p) xs)'
In the third argument of `Branch', namely
`(reconstruct drop (length (fst p) xs) (snd p))'
|
15 | reconstruct (x:xs) y = Branch (reconstruct take (length (fst p) xs) (fst p)) x (reconstruct drop (length (fst p) xs) (snd p))
| ^^^^^^^^^^^^^^^^^
[Finished in 0.5s]
答案 0 :(得分:1)
reconstruct :: [Int] -> [Int] -> IntTree
reconstruct [] [] = Empty
reconstruct (x:xs) y = let (l,_:r) = span (x /=) y
(l',r') = splitAt (length l) xs
in Branch (reconstruct l' l) x (reconstruct r' r)
reconstruct _ _ = error "incomplete pattern"
这似乎可以在我尝试过的单个测试用例上运行,并且几乎是您打算编写的(我认为)。如果节点可以具有与自己相同的内容的后代(?),则会遇到问题。我认为它可能会遍历l
(由于length
)两次,您可以用zip
和一些其他逻辑来解决这个问题。
答案 1 :(得分:0)
函数“ reconstruct”应用于三个参数,但是其类型“ [Int]-> [Int]-> IntTree”只有两个
(reconstruct take (length (fst p) xs) (fst p))
您将reconstruct
函数应用于3个参数,如错误消息中所述:take
,(length (fst p) xs)
和(fst p)
。
类似的错误与长度应用程序有关:您将2个参数传递给它。
也许您打算将FUNCTION(ARGUMENT)
作为单个argumetn传递给下一个函数。它不能像这样工作,它将被视为2个参数:FUNCTION
和(ARGUMENT)
。相反,您应该使用(FUNCTION ARGUMENT)
,如果参数很复杂,则应该使用(FUNCTION (ARGUMENT))
。
此外,您不应将函数的参数与函数take (length ... LIST)
分开进行分组。这被视为单个参数(length ... LIST)
。它们应与该功能位于同一括号中。
因此,您的第一个重建呼叫看起来像:
(reconstruct (take (length (fst p)) xs) (fst p))
也许其余的表达式也有类似的问题