Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
F# 读取未知行数,并计算f中每行的总和#_F# - Fatal编程技术网

F# 读取未知行数,并计算f中每行的总和#

F# 读取未知行数,并计算f中每行的总和#,f#,F#,假设我有n(在本例中为4)个输入 5 2 3 2 1 8 9 0 如何读取所有输入并将每行的总和存储在列表/数组中?首先,您需要一个函数来解析和求和一行: open System let parseAndSumLine (s: string) = let numbers = s.Split ' ' Int32.Parse numbers.[0] + Int32.Parse numbers.[1] 使用此功能,您可以使用file.ReadAllLines读取文件中的所有行,并

假设我有n(在本例中为4)个输入

5 2
3 2
1 8
9 0

如何读取所有输入并将每行的总和存储在列表/数组中?

首先,您需要一个函数来解析和求和一行:

open System

let parseAndSumLine (s: string) =
    let numbers = s.Split ' '
    Int32.Parse numbers.[0] + Int32.Parse numbers.[1]
使用此功能,您可以使用
file.ReadAllLines
读取文件中的所有行,并通过
parseAndSumLine
映射结果:

open System.IO

let sums =
    File.ReadAllLines "path/to/file"
    |> Array.map parseAndSumLine

首先,您需要一个函数来解析和求和一行:

open System

let parseAndSumLine (s: string) =
    let numbers = s.Split ' '
    Int32.Parse numbers.[0] + Int32.Parse numbers.[1]
使用此功能,您可以使用
file.ReadAllLines
读取文件中的所有行,并通过
parseAndSumLine
映射结果:

open System.IO

let sums =
    File.ReadAllLines "path/to/file"
    |> Array.map parseAndSumLine

您的程序是如何输入的?从一个文件,从命令行读取,作为一个函数的参数输入…?输入是如何进入你的程序的?从文件中,从命令行读取,作为参数提供给函数。。。?