Search code examples
haskellparsec

Haskell Parsec strange issue with multiple expression occurrences


here is the code which to my mind shouldn't cause any issue but for some reason does?

    program = expr8
        <|> seqOfStmt  

    seqOfStmt = 
        do list <- (sepBy1 expr8 whiteSpace)
            return $ if length list == 1 then head list else Seq list

I get 3 errors all in respect to 'list' not being in scope? It's probably blatantly obvious what is going wrong but I can't figure out why

If there are any alternatives to this I would greatly like to hear them !

Thanks in advance, Seán


Solution

  • Your final line uses a tab character for indentation, while the other lines use spaces only.

    You have tabs set to four spaces in your editor, but ghc uses eight character tab stops (just as terminals do).

    Therefore your return line is parsed as a continuation of the previous line, and list is not yet in scope.

    One easy way to fix this is to refrain from using tabs: use spaces only.


    Once you've fixed that, your next error will probably be a type error: head list and Seq list have different types (unless perhaps you have redefined head for some reason). It's not clear why you want to treat the list differently if it contains only a single element.