
winSSQ count noRed noBlue = do { yesRed <- [1..33] \\ noRed; yesBlue <- [1..16] \\ noBlue; bracket (openFile "ssqNum.txt" WriteMode) (hClose) (\hd1 -> pickSSQ count yesRed yesBlue hd1); return () } will report: Couldn't match expected type `IO ()' against inferred type `[()]' In a stmt of a 'do' expression: bracket (openFile "ssqNum.txt" WriteMode) (hClose) (\ hd1 -> pickSSQ count yesRed yesBlue hd1) However, the following works fine: winSSQ count noRed noBlue = do let yesRed = [1..33] \\ noRed let yesBlue = [1..16] \\ noBlue bracket (openFile "ssqNum.txt" WriteMode) (hClose) (\hd1 -> pickSSQ count yesRed yesBlue hd1) Why ? -- View this message in context: http://www.nabble.com/How-to-use-%22bracket%22-properly---tp25953522p2595352... Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

They're "in" different Monads. The first one does x <- [...], which
means that you're operating in the list Monad instance, and bracket
operates in the IO Monad. The second one uses let x = [...] which
doesn't have any effect on what Monad you're in, so the whole thing
can be in IO.
Note that when you do x <- [1..3]; y <- [4..6] you're going to get all
9 pairs of values from x and y, by the way.
Hope this helps,
Dan
On Mon, Oct 19, 2009 at 1:33 AM, zaxis
winSSQ count noRed noBlue = do { yesRed <- [1..33] \\ noRed; yesBlue <- [1..16] \\ noBlue; bracket (openFile "ssqNum.txt" WriteMode) (hClose) (\hd1 -> pickSSQ count yesRed yesBlue hd1); return () } will report: Couldn't match expected type `IO ()' against inferred type `[()]' In a stmt of a 'do' expression: bracket (openFile "ssqNum.txt" WriteMode) (hClose) (\ hd1 -> pickSSQ count yesRed yesBlue hd1)
However, the following works fine:
winSSQ count noRed noBlue = do let yesRed = [1..33] \\ noRed let yesBlue = [1..16] \\ noBlue bracket (openFile "ssqNum.txt" WriteMode) (hClose) (\hd1 -> pickSSQ count yesRed yesBlue hd1)
Why ? -- View this message in context: http://www.nabble.com/How-to-use-%22bracket%22-properly---tp25953522p2595352... Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

thanks for your quick answer. But winSSQ count noRed noBlue = do { let yesRed = [1..33] \\ noRed; let yesBlue = [1..16] \\ noBlue; bracket (openFile "ssqNum.txt" WriteMode) (hClose) (\hd1 -> pickSSQ count yesRed yesBlue hd1); } will report: parse error on input `let' Daniel Peebles wrote:
They're "in" different Monads. The first one does x <- [...], which means that you're operating in the list Monad instance, and bracket operates in the IO Monad. The second one uses let x = [...] which doesn't have any effect on what Monad you're in, so the whole thing can be in IO.
Note that when you do x <- [1..3]; y <- [4..6] you're going to get all 9 pairs of values from x and y, by the way.
Hope this helps, Dan
On Mon, Oct 19, 2009 at 1:33 AM, zaxis
wrote: winSSQ count noRed noBlue = do { yesRed <- [1..33] \\ noRed; yesBlue <- [1..16] \\ noBlue; bracket (openFile "ssqNum.txt" WriteMode) (hClose) (\hd1 -> pickSSQ count yesRed yesBlue hd1); return () } will report: Couldn't match expected type `IO ()' against inferred type `[()]' In a stmt of a 'do' expression: bracket (openFile "ssqNum.txt" WriteMode) (hClose) (\ hd1 -> pickSSQ count yesRed yesBlue hd1)
However, the following works fine:
winSSQ count noRed noBlue = do let yesRed = [1..33] \\ noRed let yesBlue = [1..16] \\ noBlue bracket (openFile "ssqNum.txt" WriteMode) (hClose) (\hd1 -> pickSSQ count yesRed yesBlue hd1)
Why ? -- View this message in context: http://www.nabble.com/How-to-use-%22bracket%22-properly---tp25953522p2595352... Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- View this message in context: http://www.nabble.com/How-to-use-%22bracket%22-properly---tp25953522p2595371... Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

thanks for your quick answer. But I think he actually answered your question. I.e. try it with this extra 'do' statement:
winSSQ count noRed noBlue = do { do let yesRed = [1..33] \\ noRed; let yesBlue = [1..16] \\ noBlue; bracket (openFile "ssqNum.txt" WriteMode) (hClose) (\hd1 -> pickSSQ count yesRed yesBlue hd1); } In the future please use complete examples that have all variables and import statements. It helps us help you. Cheers, Thomas

The original code is: winSSQ count noRed noBlue = do let yesRed = [1..33] \\ noRed let yesBlue = [1..16] \\ noBlue bracket (openFile "ssqNum.txt" WriteMode) (hClose) (\hd1 -> pickSSQ count yesRed yesBlue hd1) It works very well. However, as i am used to C style so i want convert it into winSSQ count noRed noBlue = do { let yesRed = [1..33] \\ noRed; let yesBlue = [1..16] \\ noBlue; bracket (openFile "ssqNum.txt" WriteMode) (hClose) (\hd1 -> pickSSQ count yesRed yesBlue hd1); } or winSSQ count noRed noBlue = do { yesRed <- [1..33] \\ noRed; yesBlue <- [1..16] \\ noBlue; bracket (openFile "ssqNum.txt" WriteMode) (hClose) (\hd1 -> pickSSQ count yesRed yesBlue hd1); } but all fail ! Thomas DuBuisson wrote:
thanks for your quick answer. But I think he actually answered your question. I.e. try it with this extra 'do' statement:
winSSQ count noRed noBlue = do { do let yesRed = [1..33] \\ noRed; let yesBlue = [1..16] \\ noBlue; bracket (openFile "ssqNum.txt" WriteMode) (hClose) (\hd1 -> pickSSQ count yesRed yesBlue hd1); }
In the future please use complete examples that have all variables and import statements. It helps us help you.
Cheers, Thomas _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- View this message in context: http://www.nabble.com/How-to-use-%22bracket%22-properly---tp25953522p2595394... Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

zaxis wrote:
It works very well. However, as i am used to C style so i want convert it into
winSSQ count noRed noBlue = do { let yesRed = [1..33] \\ noRed; let yesBlue = [1..16] \\ noBlue; bracket (openFile "ssqNum.txt" WriteMode) (hClose) (\hd1 -> pickSSQ count yesRed yesBlue hd1); }
You need to put brackets around the declarations in a let as well: winSSQ count noRed noBlue = do { let { yesRed = [1..33] \\ noRed }; let { yesBlue = [1..16] \\ noBlue }; bracket (openFile "ssqNum.txt" WriteMode) (hClose) (\hd1 -> pickSSQ count yesRed yesBlue hd1); } Regards, apfelmus -- http://apfelmus.nfshost.com

oh! thanks! But why ? Heinrich Apfelmus wrote:
zaxis wrote:
It works very well. However, as i am used to C style so i want convert it into
winSSQ count noRed noBlue = do { let yesRed = [1..33] \\ noRed; let yesBlue = [1..16] \\ noBlue; bracket (openFile "ssqNum.txt" WriteMode) (hClose) (\hd1 -> pickSSQ count yesRed yesBlue hd1); }
You need to put brackets around the declarations in a let as well:
winSSQ count noRed noBlue = do { let { yesRed = [1..33] \\ noRed }; let { yesBlue = [1..16] \\ noBlue }; bracket (openFile "ssqNum.txt" WriteMode) (hClose) (\hd1 -> pickSSQ count yesRed yesBlue hd1); }
Regards, apfelmus
-- http://apfelmus.nfshost.com
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- View this message in context: http://www.nabble.com/How-to-use-%22bracket%22-properly---tp25953522p2595693... Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

On Mon, Oct 19, 2009 at 1:44 PM, zaxis
oh! thanks! But why ?
A let can introduce multiple declarations. So this foo = do let x = 3 let y = 4 return $ x+ y can also be written like foo = do let x = 3 y = 4 -- no let return $ x + y With explicit blocks: foo = do { let {x = 3; y = 4;}; return $ x + y; }

On Mon, Oct 19, 2009 at 6:10 AM, Roel van Dijk
On Mon, Oct 19, 2009 at 1:44 PM, zaxis
wrote: oh! thanks! But why ?
A let can introduce multiple declarations.
So this
foo = do let x = 3 let y = 4 return $ x+ y
can also be written like
foo = do let x = 3 y = 4 -- no let return $ x + y
To be clear, the reason this breaks is because this is a valid let syntax: let x = 1 ; y = 2 in x + y See the semicolon? So when you put a let in a block, and it sees the semicolon at the end of the line, it is expecting another let binding. If there's a newline, then the layout rule applies and the next line is considered the start of a new layout block, even though it's at the same level. So, basically, everything gets borked up.
With explicit blocks:
foo = do { let {x = 3; y = 4;}; return $ x + y; } _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

zaxis wrote:
oh! thanks! But why ?
The gory details can be found in the Haskell 98 Report: syntax of do expressions http://www.haskell.org/onlinereport/exps.html#do-expressions syntax of decls http://www.haskell.org/onlinereport/decls.html Details for the layout rule http://www.haskell.org/onlinereport/lexemes.html#sect2.7 Regards, apfelmus -- http://apfelmus.nfshost.com

zaxis
winSSQ count noRed noBlue = do let yesRed = [1..33] \\ noRed let yesBlue = [1..16] \\ noBlue bracket (openFile "ssqNum.txt" WriteMode) (hClose) (\hd1 -> pickSSQ count yesRed yesBlue hd1)
It works very well. However, as i am used to C style so i want convert it into
winSSQ count noRed noBlue = do { let yesRed = [1..33] \\ noRed;
^^^ ^ Didn't you just comment out your semicolons?
let yesBlue = [1..16] \\ noBlue; bracket (openFile "ssqNum.txt" WriteMode) (hClose) (\hd1 -> pickSSQ count yesRed yesBlue hd1); }
-k -- If I haven't seen further, it is by standing in the footprints of giants

On Mon, Oct 19, 2009 at 9:18 AM, Ketil Malde
zaxis
writes: winSSQ count noRed noBlue = do let yesRed = [1..33] \\ noRed let yesBlue = [1..16] \\ noBlue bracket (openFile "ssqNum.txt" WriteMode) (hClose) (\hd1 -> pickSSQ count yesRed yesBlue hd1)
It works very well. However, as i am used to C style so i want convert it into
winSSQ count noRed noBlue = do { let yesRed = [1..33] \\ noRed;
^^^ ^ Didn't you just comment out your semicolons?
Hi Ketil, That's the first thing I thought when I read this code, then I realised.... those aren't comment delimiters in Haskell! :-) D -- Dougal Stanton dougal@dougalstanton.net // http://www.dougalstanton.net

On Sun, Oct 18, 2009 at 11:50 PM, zaxis
The original code is:
winSSQ count noRed noBlue = do let yesRed = [1..33] \\ noRed let yesBlue = [1..16] \\ noBlue bracket (openFile "ssqNum.txt" WriteMode) (hClose) (\hd1 -> pickSSQ count yesRed yesBlue hd1)
It works very well. However, as i am used to C style so i want convert it into
In the long run it is probably easier to just get used to haskell style. Putting in fewer braces and semicolons will be the least of the adjustments to make from C. I've seen some brace-ful code out there (notably ghc) but it tends not to be in C style even so, e.g. with leading ;s instead of trailing ones. Even though haskell style is already all over the map, using layout and omitting the punctuation seems most widespread to me. Or maybe it's just my bias since I prefer that style myself. It might be interesting to have a few "standard" styles documented, not to try to enforce anything, but have a couple self-consistent systems up there with rationale for otherwise undecided people to look at. E.g. after seeing the leading comma style around I tried it out myself and eventually adopted it, even though it looked weird at first. But it was only after seeing code in that style and guessing for myself why it was that way.

zaxis schrieb:
winSSQ count noRed noBlue = do { yesRed <- [1..33] \\ noRed; yesBlue <- [1..16] \\ noBlue; bracket (openFile "ssqNum.txt" WriteMode) (hClose) (\hd1 -> pickSSQ count yesRed yesBlue hd1); return ()
You might prefer 'withFile' or even better and if possible, write pickSSQ without IO interaction, just producing a String lazily and write this using writeFile.
participants (11)
-
Daniel Peebles
-
Dougal Stanton
-
Evan Laforge
-
Heinrich Apfelmus
-
Henning Thielemann
-
Ketil Malde
-
Luke Palmer
-
Miguel Mitrofanov
-
Roel van Dijk
-
Thomas DuBuisson
-
zaxis