[Haskell-beginners] Parsing a file

Francesco Ariis fa-ml at ariis.it
Wed Jan 26 12:58:41 UTC 2022


Hello Roger,

Il 26 gennaio 2022 alle 08:40 Roger Mason ha scritto:
> I attempt to parse the input:
> ======================================================
>    :{
>      main = do
>      eqatoms <- readFile "Parsing_File/eqatoms.out"
>      Parsec.parse species "test species" eqatoms
>      return
>    :}
> 
> Prelude Parsec Text.Parsec Control.Applicative Control.Monad.Identity| Prelude Parsec Text.Parsec Control.Applicative Control.Monad.Identity| Prelude Parsec Text.Parsec Control.Applicative Control.Monad.Identity| Prelude Parsec Text.Parsec Control.Applicative Control.Monad.Identity| Prelude Parsec Text.Parsec Control.Applicative Control.Monad.Identity| 
> <interactive>:250:3: error:
>     ,* Couldn't match type `Either Parsec.ParseError' with `IO'
>       Expected type: IO (String, String)
>         Actual type: Either Parsec.ParseError (String, String)
>     ,* In a stmt of a 'do' block:
>         Parsec.parse species "test species" eqatoms
>       In the expression:
>         do eqatoms <- readFile "Parsing_File/eqatoms.out"
>            Parsec.parse species "test species" eqatoms
>            return
>       In an equation for `main':
>           main
>             = do eqatoms <- readFile "Parsing_File/eqatoms.out"
>                  Parsec.parse species "test species" eqatoms
>                  return

The problem is with this line

>      Parsec.parse species "test species" eqatoms

`parse` returns an Either, so you should pattern match on
its `Left` and `Right` (using `case` or the `either` function).
This has to be done inside a `let` too, because parse is a pure
function.
Does that help?
—F


More information about the Beginners mailing list