解析maxheap函数中的错误

时间:2017-02-19 13:59:34

标签: haskell

我是Haskell的新手,现在正在树上工作。在这里,我想创建一个函数,它交换树中的元素,直到它成为最大值。然后,我将使用Heapsort算法。

data Bintree a = E | Tree (Bintree a) a (Bintree a) deriving( Eq, Ord, Show )

swaptree:: Bintree Int -> Bintree Int
swaptree (Tree E w E) = (Tree E w E)
swaptree (Tree E w (Tree lr x rr))|((max w x)==w)=(Tree E w (Tree lr x rr))
                              |((max w x)==x)=(Tree E x(swaptree(Tree lr w rr)))
                              |otherwise error "something went wrong or program is bad"
swaptree (Tree (Tree ll v rl) w E)|((max v w)==w)=(Tree (Tree ll v rl) w E)
                              |((max v w)==v)=(Tree (swaptree(Tree ll w rl)) v E)
                              |otherwise error "something went wrong or program is bad"
swaptree (Tree (Tree ll v rl) w (Tree lr x rr) )|((max3 v w x)==w) = (Tree (Tree ll v rl) w (Tree lr x rr)) 
                                            |((max v x)==x)=(Tree (Tree ll v rl) x (swaptree(Tree lr w rr)))
                                            |((max v x)==v)=(Tree(swaptree(Tree ll w rl)) v (Tree lr x rr) )
                                            |otherwise error "something went wrong or program is bad"
swaptree':: Bintree Int -> Bintree Int
swaptree' (Tree E w E) = (Tree E w E)
swaptree' (Tree lub w rub)  = (swaptree(Tree(swaptree lub) w (swaptree rub)))

这给我带来了这个错误:

   8:1: error:parse error (possibly incorrect indentation or mismatched brackets)

现在,我查了一下并阅读,这通常会发生,leSt没有使用in,但由于我没有使用它,我不知道如何解决这个问题。 我非常感谢有关如何改进我的代码的任何帮助或提示!

PS:请怜悯我。正如我所说,我目前在Haskell上非常糟糕。

2 个答案:

答案 0 :(得分:1)

|otherwise error "something went wrong or program is bad"

应该是

|otherwise = error "something went wrong or program is bad"

答案 1 :(得分:1)

这个错误已经被李尧瑶发现,并且提出了修复建议。

请注意,您还可以使用以下样式删除违规行:

// create a FileHelpers class with a comma delimiter.
DelimitedClassBuilder cb = new DelimitedClassBuilder("Person", ",");
// add your fields based on whatever logic you like (e.g., maybe read the column names from the first row)
cb.AddField("firstName", typeof(string));
cb.AddField("lastName", typeof(string));
cb.LastField.FieldNullValue = "default last name";

// create your import engine from the class you created above.
DelimitedFileEngine engine = new DelimitedFileEngine(cb.CreateRecordClass());
DataTable dt = engine.ReadFileAsDT("data.csv"); 

确实,检查三个个案swaptree (Tree E w (Tree lr x rr)) | x <= w = Tree E w (Tree lr x rr) | otherwise = Tree E x (swaptree (Tree lr w rr)) -- etc. max x w == xmax x w == w是很奇怪的。如果我们确实达到了“其他”,那么otherwise的定义就会出现问题。我们可以放心地忽略这种情况。