[Haskell-beginners] Parse error in pattern

Daniel Fischer daniel.is.fischer at web.de
Fri Feb 26 12:12:24 EST 2010


Am Freitag 26 Februar 2010 17:37:30 schrieb Magnus Therning:
> It doesn't look like a complete piece of code so these comments aren't
> backed up by running it through GHCi or anything.
>
> On Fri, Feb 26, 2010 at 16:29, Florian Duret <flo.morphe at gmail.com> 
wrote:
> > Hello,
> >
> >
> > I try to set up a verification on the number of arguments given to my
> > program, but keep on getting "Parse error in pattern"
> > Here is what my code looks like:
> > main :: IO()
> > main = do
> >     -- On commence par ouvrir le fichier SAC en mode binaire
> >     argsList <- getArgs
> >     if (length (argsList) == 0)

It's most likely harmless for argument lists (although there are other 
cases), but

Don't Use

if (length list == 0)

Never. Jamais. Niemals.

Use

if (null list)

length has to traverse the entire list, which may take a long time.

> >        then do
> >              putStrLn $ "No filename given to the program.\n $
> > ProgramName file.sac"
> >              return ()
>
> I believe the 'do' here is unecessary.
>

As soon as the unnecessary "return ()" is removed.

> >       else
> >              sacFile1 <- openBinaryFile fileToOpen ReadMode
>
> Here you do need a 'do' though, I believe.
>

Yes. If he binds the name sacFile1 to a value, there must come more 
statements after it, so the "do" is required.

But it might also be wrong indentation, if the mail programme fiddled with 
that.

> > ghci complains, and tells "Parse error in pattern", indicating the
> > 'if' line number.

Invoke ghci with

$ ghci -ferror-spans file

to see how far GHC thinks the erroneous pattern extends. From that, one can 
often deduce better what the problem is.

> > Can you please help ?
> > Thank you very much,
> > Florian



More information about the Beginners mailing list