1
votes

I've been looking for an error quite a while now. Haskell complains about "parse error (possibly incorrect indentation or mismatched brackets)". I could'nt find anything. Is there anyone who can help me? The code should add and subtract binary numbers.

binadd :: [Int] -> [Int] -> [Int]
binadd xs ys = revers (binadd'(reverse xs) (reverse ys) 0) 
                       where binadd' [] [] _         = []
                             binadd' (x:xs) (y:ys) u = (mod (x+y+u) : binadd' xs ys) (div(x+y+z)2)
                             binadd' _ _ _           = error "Listen verschieden lang"


binsub :: [Int] -> [Int] -> [Int]
binsub a b = binadd a (help b) 
                 where help list = binadd [000000001] ([kipp i | i <- list ])
                       kipp    1 = 0
                       kipp    0 = 1
1
You're aware that [000000001] is the same as [1], right? Apart from that, mod needs two arguments but you've only given it (x+y+u) and expect the result to be a number. — Anyway... please add the actual error message to such a question!leftaroundabout
It looks like you're using tabs for indentations. I would suggest using spaces instead, as GHC might see tabs differently than you have your editor configured for. Most editors support the option to insert spaces when you hit the tab key. This is a common source of parse errors for beginners that I see on SO at least 4 or 5 times a week. While it's possible to use tabs for indentation, I guarantee that you'll save yourself a lot of trouble by just switching to spaces.bheklilr
It is much more helpful if you include the exact error message, like leftaroundabout said. The error message contains useful information about exactly what went wrong. As it stands, this is a bit like emailing your doctor saying "part of me has a sharp pain. Please prescribe something to make it better.". Not including the error message when asked is like not answering the doctor's questions about your pain.AndrewC
@bheklilr: It's not the first time indentations break his code.Zeta
Note that even if someone manages to use tabs correctly in their Haskell code, it will still make a mess of code when they paste it into SO, because SO shows it as 4 spaces while Haskell interprets it as 8.Ørjan Johansen

1 Answers

2
votes

Indentation is one thing that's not wrong with the code as it appears here. I suspect you have tabs in the original file you tried to compile, right? Always indent with spaces, not tabs, this is so much more reliable. You can just copy & paste back the code as it appears here, doesn't have that problem.

Frankly, the code has quite a lot of other problems though. For starters, you've misspelled reverse as revers, and used a z variable that's not defined anywhere (you mean div (x+y+u) 2). Then you've forgotten to write the 2 in mod (x+y+u) 2. BTW, Haskellers will usually write functions like div and mod in infix notation:

                         ... = ((x+y+u)`mod`2 : binadd' xs ys) ((x+y+u)`div`2)

For some reason, this reads more fluently IMO and also doesn't let you forget arguments as easily.

The line still isn't right: you have two disjoint parenthesised groups ((x+y+u)`mod`2 : binadd' xs ys) and ((x+y+u)`div`2). Putting two enclosed expressions right one after another always means the first is a function and the second is an argument to that function. But not in this case: ((x+y+u)`mod`2 : ...) can only be a list, not a function. However, binadd' xs ys is still a function because once again you've forgotten an argument. That argument is the other paren-block: you want binadd' xs ys ((x+y+u)`div`2). If you put the head in front of that you can completely drop the outer parens:

                         ... = ( (x+y+u)`mod`2 : binadd' xs ys ((x+y+u)`div`2) )
                             ≡ (x+y+u)`mod`2 : binadd' xs ys ((x+y+u)`div`2)

There's probably still more problems, but please try to find them yourself!