qwe*_*qwe 4 f# c#-to-f# f#-3.0
Say I have n (7 in this case) inputs given
10
20
30
40
50
60
70
如何读取所有输入并将它们存储在列表/数组中?
我试过这个,
let inputList = [
    while (let line = Console.ReadLine()) <> null do
        line |> int
]
Run Code Online (Sandbox Code Playgroud)
我的想法是阅读,直到我得到一个空行.
但我得到以下错误,
这个'let'之后的块未完成.期待一个表达.
要以功能样式执行此操作,您可以使用Seq.initInfinite从控制台创建序列.然后,当您使用null值时,需要终止此列表Seq.takeWhile.除此之外,您还可以使用所有Seq可用的模块功能,包括Seq.toList.
let read _ = Console.ReadLine()
let isValid = function null -> false | _ -> true
let inputList = Seq.initInfinite read |> Seq.takeWhile isValid |> Seq.toList
Run Code Online (Sandbox Code Playgroud)
        |   归档时间:  |  
           
  |  
        
|   查看次数:  |  
           853 次  |  
        
|   最近记录:  |