[Haskell-cafe] How to use "bracket" properly ?

zaxis z_axis at 163.com
Mon Oct 19 02:10:51 EDT 2009


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 <z_axis at 163.com> 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---tp25953522p25953522.html
>> Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.
>>
>> _______________________________________________
>> Haskell-Cafe mailing list
>> Haskell-Cafe at haskell.org
>> http://www.haskell.org/mailman/listinfo/haskell-cafe
>>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at 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---tp25953522p25953716.html
Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.



More information about the Haskell-Cafe mailing list