
Hi all, I am having problems adding multiple definitions with where for example in my code --A parser for recognising binary operations parseBinaryOp :: String -> String -> [(Expr, Expr, String)] parseBinaryOp op str | (elem op binops) && (notElem '(' (snd bm)) && (notElem ')' (snd bm)) && (elem nstr!!1 binops) = [(EInt 1, EInt 1, "HERE!")] | otherwise = [] where bm = bracketMatch str nstr = words (snd (bracketMatch str)) I get the error message parse error on input `=' Essentially this function is supposed to parse binary operations in a string, nstr is just a [String], binops is a list of Strings. The types appear to be fine, and GHCI dosnt say that this is the problem, the problem seems to lie with that definition of nstr after the definition of bm. I believe I have followed the definitions correctly, so I am at a loss for how to solve this problem. The list that is given for the first case is only a placeholder, once I get past this problem I should be able to make the function operate properly Any help would be much appreciated, please tell me if you need more info. Thankyou :-) -- View this message in context: http://www.nabble.com/Multiple-statements-with-Where-tp14397482p14397482.htm... Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

insertjokehere wrote:
Hi all, I am having problems adding multiple definitions with where for example in my code
--A parser for recognising binary operations parseBinaryOp :: String -> String -> [(Expr, Expr, String)] parseBinaryOp op str | (elem op binops) && (notElem '(' (snd bm)) && (notElem ')' (snd bm)) && (elem nstr!!1 binops) = [(EInt 1, EInt 1, "HERE!")] | otherwise = [] where bm = bracketMatch str nstr = words (snd (bracketMatch str))
alignment. Where clauses are layout. Here is how I suggest you layit out: --A parser for recognising binary operations parseBinaryOp :: String -> String -> [(Expr, Expr, String)] parseBinaryOp op str | (elem op binops) && (notElem '(' (snd bm)) && (notElem ')' (snd bm)) && (elem nstr!!1 binops) = [(EInt 1, EInt 1, "HERE!")] | otherwise = [] where bm = bracketMatch str nstr = words (snd (bracketMatch str)) Note that the where clause comes to the left of the |, because where clauses scope over all the guards It may be a good idea not to use 'hard' tabs. Jules

insertjokehere wrote:
--A parser for recognising binary operations parseBinaryOp :: String -> String -> [(Expr, Expr, String)] parseBinaryOp op str | (elem op binops) && (notElem '(' (snd bm)) && (notElem ')' (snd bm)) && (elem nstr!!1 binops) = [(EInt 1, EInt 1, "HERE!")]
You want (elem (nstr !! 1) binops) here because function application binds stronger then all operators. You can even write elem op binops && notElem '(' (snd bm) && ... for that reason.
| otherwise = [] where bm = bracketMatch str nstr = words (snd (bracketMatch str))
You want where bm = ... nstr = ... here, because the first non-space characters of lines belonging to the same layout-block have to be at the same horizontal position. Your where bm = bracketMatch ... nstr = ... is parsed as where { bm = bracketMatch ... nstr = ... } instead of where { bm = bracketMatch ...; nstr = ... } Tillmann

insertjokehere wrote:
where bm = bracketMatch str nstr = words (snd (bracketMatch str))
It looks like you have set your editor to make tabs look like four spaces. Haskell compilers are required to interpret tabs as being equivalent to eight spaces, so it sees "bm =" and "nstr =" at different alignments. Moral of the story: It's probably best to just not use tabs in Haskell code.
participants (4)
-
insertjokehere
-
Jules Bean
-
Matthew Brecknell
-
Tillmann Rendel