【发布时间】:2011-10-02 00:46:52
【问题描述】:
这是我的代码:
module RTX
open System
open System.Text.RegularExpressions
let input = "Line 1: Num allocs:3 New:2 Delete:1
Line 2: Num allocs:3 New:2 Delete:1
Line 3: Num allocs:3 New:2 Delete:1"
type AllocResult = { lineNo:string; numAllocs:string; news:string; frees:string }
let matches = Regex.Matches(input,@"Line (\d+): Num allocs:(\d+) New:(\d+) Delete:(\d+)",RegexOptions.Singleline)
let matchCollection = [ for m in matches do if m.Success then yield [for l in m.Groups -> l.Value]]
let allocCreator (e: string list) = { lineNo = e.[0]; numAllocs = e.[1]; news = e.[2]; frees = e.[3] }
let wfirst = List.map allocCreator List.map List.tail matchCollection
List.iter (fun e -> printfn "%s\n") wfirst
//List.iter (printfn "%s\n") matchCollection
Console.ReadLine()
我收到的错误是:This value is not a function and cannot be applied (FS0003),它出现在List.map allocCreator 部分。基本上,我想从 Match 中删除匹配的字符串,并且只保留记录中的捕获。
编辑:我将尝试多解释一下我想要实现的目标。匹配结果将是这样的:
[ "Line 1: Num allocs:3 New:2 Delete:1"; "1"; "3"; "2"; "1"; ]
通过使用
let matchCollection = [ for m in matches do if m.Success then yield [for l in m.Groups -> l.Value]],我试图获取列表列表,如下所示:
[["Line 1: Num allocs:3 New:2 Delete:1"; "1"; "3"; "2"; "1"; ];["Line 2: Num allocs:3 New:2 Delete:1";"2"; "3"; "2"; "1"; ]; ["Line 3: Num allocs:3 New:2 Delete:1";"3"; "3"; "2"; "1"]]
通过使用List.map List.tail matchCollection,我试图从上面的列表中获取:
[["1"; "3"; "2"; "1"; ];["2"; "3"; "2"; "1"; ]; [;"3"; "3"; "2"; "1"]]
通过使用List.map allocCreator,我试图将上述列表转换为记录列表。我是 F# 的新手,可能我的逻辑是错误的,但我不明白为什么/在哪里。
编辑 2:这似乎是一个优先问题。如果我这样做:
let wfirst = List.map allocCreator (List.map List.tail matchCollection);;
然后我得到了我需要的东西。
【问题讨论】:
标签: f#