F#中的布尔值被错误地评估

时间:2015-03-12 09:30:01

标签: .net f#

我有这段F#代码:

let readBytes isVariable (br: BinaryReader) = 
    seq {
        while br.BaseStream.Position < br.BaseStream.Length do
           let c = br.PeekChar()
           if c = 0 && isVariable
               then
                   yield Array.get (br.ReadBytes(2)) 1
               else yield br.ReadByte()
       }

let main argv = 
    let processCopybook = readLines copybookFile
                          |> Seq.map (fun x -> parseElement' x parserList)
                          |> Seq.filter (fun s -> not (s = []))
                          |> Seq.map makeElementFromTokenLine
                          |> makeTree
    let isVariable = isVariableLength processCopybook

    let br = lazy(new BinaryReader(new FileStream(dataFile, FileMode.Open)))

    let bytes = readBytes isVariable br.Value

    if isVariable
       then Seq.takeWhile (fun c -> not (c = byte 0x0A)) bytes |> Seq.toList |> ignore
       else ()

当我运行此代码时,无论isVariable的值(是true还是false),它都会进入最后一个if语句的then子句

但是,当我将这段代码用于if语句时:

let func = if isVariable
              then fun () -> Seq.takeWhile (fun c -> not (c = byte 0x0A)) bytes |> Seq.toList |> ignore
              else fun () -> ()

然后调用func(),它被正确评估。

这是为什么?它似乎没有任何意义。

0 个答案:

没有答案