<div>Thank you Daniel, this helps a lot !<br></div><div><br></div><div>As you can guess, I come from the imperative world, but want to discover functionnal languages (thanks to xmonad).</div><div><br></div><div>Thank you very much for your explainations. I think I will fall in that trap again, but next time, I should be able to fix it myself. I still don't understand exactly what lies behind, but start to get the picture. I didn't change anything besides the position of the 'do' and the deletion of the 'return ()'.</div>
<div>I will modify the code with the realWork function as you suggested. I looks much nicer.</div><div><br></div><div>As of the last problem, with the intToDigit function. I already knew about the bug. I actually knew it wouldn't work since it is takes only argument from 0 to 15... Well, that was lazy from me. I just wanted to write the statement so that I don't forget it.</div>
<div><br></div><div>Danke sehr.</div><div><br></div><div><br></div><br><div class="gmail_quote">2010/2/26 Daniel Fischer <span dir="ltr"><<a href="mailto:daniel.is.fischer@web.de">daniel.is.fischer@web.de</a>></span><br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">Am Freitag 26 Februar 2010 18:34:35 schrieb Florian Duret:<br>
<div class="im">> It seems that both your suggestions have worked ! Thank you very much.<br>
> But I still can't figure out what went wrong.<br>
<br>
</div>Did you change anything besides removing the "do" and "return ()" from the<br>
then-branch and insert the "do" in the else-branch?<br>
If not, you've been bitten by a somewhat less than obvious aspect of layout<br>
- although it's pretty clear with an explanation.<br>
A new layout-block isn't opened by a larger indentation, but by the<br>
keywords do, let, of and where.<br>
So, when you write<br>
<br>
... = do<br>
xxx<br>
if blah<br>
then do<br>
foo<br>
bar<br>
else<br>
baz<br>
zap<br>
<br>
the "do" after the then opens a new layout-block inside the big do-block,<br>
since you didn't insert an explicit brace. The indentation level thereof<br>
is determined by the 'f' of "foo", bar is indented to the same level as<br>
foo, so it's a new expression in that same block.<br>
The else is indented less than the foo, so that ends the inner layout-block<br>
and we return to the layout-block of the big do-block. The indentation<br>
level thereof is determined by the first 'x', and the "if" is indented to<br>
the same level.<br>
The else, the baz and the zap are all indented further than the if, so they<br>
all belong to the if-expression (as intended).<br>
But since there's no "do" after the "else", all that is on one logical<br>
line, it's parsed as<br>
<br>
... = do<br>
xxx<br>
if blah then do { foo; bar } else baz zap<br>
<br>
Not what was intended, but it parses just fine.<br>
<br>
Now you didn't have<br>
<br>
baz<br>
zap<br>
<br>
but<br>
<br>
baz <- bong<br>
zap<br>
<br>
and since we're still having only one logical line for the if-expression,<br>
the parser sees that as<br>
<br>
... = do<br>
xxx<br>
if blah then do { foo; bar } else baz <- bong zap<br>
<br>
But the syntax is<br>
<br>
pattern <- expression<br>
<br>
so the parser tries to parse<br>
<br>
if blah then do { foo; bar } else baz<br>
<br>
as a pattern. But it doesn't conform to the pattern productions, hence the<br>
parse error.<br>
<div class="im"><br>
><br>
> My initial goal was to keep the minimum inside the if ... then ... else<br>
> statement. Basically, if the list is empty, then stop. If not, then<br>
> assign the argument to sacFile1, and go on with the rest.<br>
<br>
</div>It would be cleanest to have<br>
<br>
realWork :: FileName -> IO ()<br>
realWork file = do<br>
sacFile1 <- openBinaryFile file ReadMode<br>
...<br>
<br>
main :: IO ()<br>
main = do<br>
argList <- getArgs<br>
case argList of<br>
[] -> putStrLn "No filename ..."<br>
(f:_) -> realWork f<br>
<br>
or<br>
<br>
if null argList<br>
then putStrLn "..."<br>
else realWork (head argList)<br>
<br>
, je pense.<br>
<div class="im"><br>
><br>
> Here is what it looks like now:<br>
> module Main () where<br>
><br>
> import System.IO<br>
> import System.Environment(getArgs)<br>
> import Data.Char(intToDigit)<br>
><br>
> import SAC_Type<br>
> import SAC_IO<br>
><br>
> main :: IO()<br>
> main = do<br>
> -- On commence par ouvrir le fichier SAC en mode binaire<br>
> argsList <- getArgs<br>
> if (null argsList)<br>
> then<br>
> putStrLn $ "No filename given to the program.\n $<br>
> ProgramName file.sac"<br>
> else do<br>
> sacFile1 <- openBinaryFile (head argsList) ReadMode<br>
><br>
> position <- hTell sacFile1<br>
> putStrLn $ "Position 1: " ++ [intToDigit( fromInteger<br>
> (position) )]<br>
><br>
> hSeek sacFile1 AbsoluteSeek 440<br>
> position2 <- hTell sacFile1<br>
> putStrLn $ "Position 2: " ++ [intToDigit( fromInteger<br>
> (position2) )]<br>
<br>
</div>I don't think that's what you really want:<br>
<br>
Prelude Data.Char> intToDigit 440<br>
*** Exception: Char.intToDigit: not a digit 440<br>
<br>
perhaps you wanted<br>
<br>
putStrLn $ "Position 2: " ++ show position2<br>
<div><div class="h5"><br>
?<br>
<br>
><br>
> -- A la fin, il faut evidemment le fermer<br>
> hClose sacFile1<br>
><br>
><br>
> Thank you, Danke, 谢谢, merci, etc...<br>
<br>
</div></div></blockquote></div><br>