Reputation: 77
I am trying to compile the following code, but am getting a "parse error on "|" in the otherwise statement"
Now I am confused as I have used pretty much similar syntax in a few other functions, the only difference being this one takes 2 elements on a list and tries to reconstruct one (FYI: Did not test it yet as I can't compile, so the code in the convertElements line might be wrong!)
I am using spaces, and everything seems aligned. Any idea as to what would be causing it? I moved it to the bottom of the where in case it helped, but alas it did not.
convertElements :: [(String, String, String)] -> [(String, String, String)]
convertElements [] = []
convertElements (x:y:xs) = x: (a,b,c) : convertElements((a,b,c) ++ xs)
where b = (getSecond y)
c = (getThird y)
a | if ((containsDash (getThird x)) > 0) then last (getThird x)
| otherwise = (getFirst y)
Upvotes: 0
Views: 1146
Reputation: 62848
You can do
a | ...condition... = ...value...
| otherwise = ...value...
or you can do
a = if ...condition... then ...value... else ...value...
But not both. Most particularly, you can't have if
without then
and else
. In Haskell, if
isn't a statement, it's an expression (rather like the C ternary operator ?:
, if you know of that).
Upvotes: 2
Reputation: 170815
To expand a bit on @ephemient's answer: |
can be followed by if ...
but that makes if
-expression the guard, so the compiler is expecting to see
a | if ... then ... else ... = ...
But the second |
shows up before else
does, so you get an error there.
Upvotes: 2
Reputation: 204926
You were close but not quite with the guard syntax, which does not use the if
/then
/else
keywords.
convertElements :: [(String, String, String)] -> [(String, String, String)]
convertElements [] = []
convertElements (x:y:xs) = x : (a,b,c) : convertElements ((a,b,c) : xs)
where b = getSecond y
c = getThird y
a | containsDash (getThird x) > 0 = last (getThird x)
| otherwise = getFirst y
Upvotes: 3
Reputation: 77
Of course it's when you ask a question that you figure out another way around... I still don't know why I had that error, however the simple solution is to use an "else" instead. Which I've done and can now fix the multitude of errors from this function.
Upvotes: 0