user2179293
user2179293

Reputation: 435

Parse error on input `|' in haskell

Why do I get parse error on input '|' in

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

I get the error on line 24 on my original code. It is line | otherwise in this piece of code.

Upvotes: 0

Views: 171

Answers (1)

user824425
user824425

Reputation:

As far as I know, you can't have where clauses specific to a guard. What you can do, is putting these where clauses under the 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

Note that this may also make some errors more evident (you had new_l = insert l x), while costing nothing (due to laziness, you don't actually build both the new_l and new_r trees).

Upvotes: 4

Related Questions