为什么输入'|'会出现解析错误在
insert::a -> Tree a -> Tree a
insert x Leaf=Node 0 Leaf x Leaf
insert x (Node h l val r)
| (getHeight l)<=(getHeight r) = Node ((getHeight new_l)+1) (new_l) val r
where
new_l = (insert l x)
| otherwise = Node ((getHeight new_r)+1) l val (new_r)
where
new_r=insert x r
我在原始代码的第24行收到错误。在这段代码中是行| otherwise
。
答案 0 :(得分:4)
据我所知,你不能拥有针对警卫的where
条款。您可以做的是将这些where
条款置于案例之下:
insert::a -> Tree a -> Tree a
insert x Leaf = Node 0 Leaf x Leaf
insert x (Node h l val r)
| getHeight l <= getHeight r = Node (getHeight new_l + 1) new_l val r
| otherwise = Node (getHeight new_r + 1) l val new_r
where
new_l = insert x l
new_r = insert x r
请注意,这也可能会使某些错误更加明显(您有new_l = insert l x
),而没有任何费用(由于懒惰,您实际上并未构建new_l
和{{1树木)。