为什么双列表(我的论点)不同意 Parse Line/Map 函数在 main 中? F#
Why does double list (my argument) not agree with Parse Line/Map function in main? F#
好的,我已经归结为最后一件事,根据 Visual Studio,它会导致主要错误,"This type was expected to have type double list
, but here has double
type." 我将向您展示有错误的代码。我有这个平均函数来计算值,它被声明为双列表。为什么它主要抱怨?如果我不清楚,我很抱歉,但我很难解释原因。 忘了一件事 - 我试图用我的程序读取文本文件。
let avg (values:double list) =
let rec sum values accum =
match values with
| [] -> accum
| head :: tail -> sum tail (accum + head/12.0)
sum values 0.0
let ReadFile filename =
[ for line in System.IO.File.ReadLines(filename) -> line ]
let ParseLine (line:string) =
let strings = line.Split('\t')
let strlist = Array.toList(strings)
let year = System.Int32.Parse(strlist.Head)
let values = List.map System.Double.Parse strlist.Tail
(year, avg values)
主要
let result = file |> List.map(fun el ->
let (year, values) = ParseLine el
(year, avg values)) //values gets the error as described above.
result |> List.iter(fun (year, avgRainfall) -> printfn "%i: %f" year avgRainfall)
我认为这在上一个问题中得到了回答,但这里是因为 let result = ... (year, avg values)
。 ParseLine
已经在计算平均值,因此它不再是一个列表,而只是一个值。
好的,在@Gary.S的帮助下,我遇到了这个。
let avg (values:double list) =
let rec sum values accum =
match values with
| [] -> accum
| head :: tail -> sum tail (accum + head/12.0)
sum values 0.0
let ReadFile filename =
[ for line in System.IO.File.ReadLines(filename) -> line ]
let ParseLine (line:string) =
let strings = line.Split('\t')
let strlist = Array.toList(strings)
let year = System.Int32.Parse(strlist.Head)
let values = List.map System.Double.Parse strlist.Tail
(year, values) // Removed Avg.
主要
let result = file |> List.map(fun el ->
let (year, values) = ParseLine el
(year, avg values)) // it works!!!
result |> List.iter(fun (year, avgRainfall) -> printfn "%A: %A" year avgRainfall)
好的,我已经归结为最后一件事,根据 Visual Studio,它会导致主要错误,"This type was expected to have type double list
, but here has double
type." 我将向您展示有错误的代码。我有这个平均函数来计算值,它被声明为双列表。为什么它主要抱怨?如果我不清楚,我很抱歉,但我很难解释原因。 忘了一件事 - 我试图用我的程序读取文本文件。
let avg (values:double list) =
let rec sum values accum =
match values with
| [] -> accum
| head :: tail -> sum tail (accum + head/12.0)
sum values 0.0
let ReadFile filename =
[ for line in System.IO.File.ReadLines(filename) -> line ]
let ParseLine (line:string) =
let strings = line.Split('\t')
let strlist = Array.toList(strings)
let year = System.Int32.Parse(strlist.Head)
let values = List.map System.Double.Parse strlist.Tail
(year, avg values)
主要
let result = file |> List.map(fun el ->
let (year, values) = ParseLine el
(year, avg values)) //values gets the error as described above.
result |> List.iter(fun (year, avgRainfall) -> printfn "%i: %f" year avgRainfall)
我认为这在上一个问题中得到了回答,但这里是因为 let result = ... (year, avg values)
。 ParseLine
已经在计算平均值,因此它不再是一个列表,而只是一个值。
好的,在@Gary.S的帮助下,我遇到了这个。
let avg (values:double list) =
let rec sum values accum =
match values with
| [] -> accum
| head :: tail -> sum tail (accum + head/12.0)
sum values 0.0
let ReadFile filename =
[ for line in System.IO.File.ReadLines(filename) -> line ]
let ParseLine (line:string) =
let strings = line.Split('\t')
let strlist = Array.toList(strings)
let year = System.Int32.Parse(strlist.Head)
let values = List.map System.Double.Parse strlist.Tail
(year, values) // Removed Avg.
主要
let result = file |> List.map(fun el ->
let (year, values) = ParseLine el
(year, avg values)) // it works!!!
result |> List.iter(fun (year, avgRainfall) -> printfn "%A: %A" year avgRainfall)