我正在使用Visual Studio 2012,并且调用的函数Console.ReadLine()将不会执行
let inSeq = readlines ()
在这个简单的程序中
open System
open System.Collections.Generic
open System.Text
open System.IO
#nowarn "40"
let rec readlines () =
seq {
let line = Console.ReadLine()
if not (line.Equals("")) then
yield line
yield! readlines ()
}
[<EntryPoint>]
let main argv =
let inSeq = readlines ()
0
Run Code Online (Sandbox Code Playgroud)
我一直在试验和研究这个,但看不出可能是一个非常简单的问题.
F#中的序列不会立即进行评估,而只是在枚举时进行评估.
这意味着readlines在您尝试使用它之前,您的函数无效.通过做某事inSeq,你将强制进行评估,这反过来会使它表现得更像你期望的.
要查看此操作,请执行一些枚举序列的操作,例如计算元素数量:
open System
open System.Collections.Generic
open System.Text
open System.IO
#nowarn "40"
let rec readlines () =
seq {
let line = Console.ReadLine()
if not (line.Equals("")) then
yield line
yield! readlines ()
}
[<EntryPoint>]
let main argv =
let inSeq = readlines ()
inSeq
|> Seq.length
|> printfn "%d lines read"
// This will keep it alive enough to read your output
Console.ReadKey() |> ignore
0
Run Code Online (Sandbox Code Playgroud)