【问题标题】:Parse error on input `|' in haskell输入“|”时解析错误在哈斯克尔
【发布时间】:2014-06-30 11:27:55
【问题描述】:

为什么在输入“|”时出现解析错误在

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 行。

【问题讨论】:

    标签: haskell


    【解决方案1】:

    据我所知,您不能有特定于警卫的where 子句。您可以做的就是将这些where 子句放在case 下:

    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_lnew_r 树)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-24
      • 1970-01-01
      相关资源
      最近更新 更多