[Haskell-beginners] Parse error in pattern

Florian Duret flo.morphe at gmail.com
Fri Feb 26 12:34:35 EST 2010


It seems that both your suggestions have worked ! Thank you very much. But I
still can't figure out what went wrong.

My initial goal was to keep the minimum inside the if ... then ... else
statement. Basically, if the list is empty, then stop. If not, then assign
the argument to sacFile1, and go on with the rest.

Here is what it looks like now:
module Main () where

import System.IO
import System.Environment(getArgs)
import Data.Char(intToDigit)

import SAC_Type
import SAC_IO

main :: IO()
main = do
     -- On commence par ouvrir le fichier SAC en mode binaire
     argsList <- getArgs
     if (null argsList)
        then
              putStrLn $ "No filename given to the program.\n $ ProgramName
file.sac"
        else do
           sacFile1 <- openBinaryFile (head argsList) ReadMode

           position <- hTell sacFile1
           putStrLn $ "Position 1: " ++ [intToDigit( fromInteger (position)
)]

           hSeek sacFile1 AbsoluteSeek 440
           position2 <- hTell sacFile1
           putStrLn $ "Position 2: " ++ [intToDigit( fromInteger (position2)
)]

           -- A la fin, il faut evidemment le fermer
           hClose sacFile1


Thank you, Danke, 谢谢, merci, etc...

2010/2/26 Daniel Fischer <daniel.is.fischer at web.de>

> 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
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20100226/30bb8500/attachment.html


More information about the Beginners mailing list