【问题标题】:How can I fix this FS00003 error?如何修复此 FS00003 错误?
【发布时间】: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#


    【解决方案1】:
    let wfirst = List.map allocCreator <| List.map List.tail matchCollection
    List.iter (printfn "%A\n") wfirst
    

    【讨论】:

    • 您能解释一下为什么您的版本有效而​​我的版本无效吗?这不是编译器看到表达式的方式:List.map(allocCreator, List.map(List.tail,matchCollection))吗?
    • 编译器将其视为:(List.map allocCreator List.map) (List.tail matchCollection),因为函数应用程序关联到左侧。
    • List.map : (('a -> 'b) -> 'a list -> 'b list) is func -> arg -> result 因为 List.map func1 List.map 的东西, (List.map func1 List.map) 部分应该是 'b list, ('b list) 有些东西正试图执行,所以编译器说“这个值('b list) 不是一个函数,不能应用”,所以你需要通过use()改变执行顺序。
    【解决方案2】:

    不确定你想用那条线实现什么,但让我们看看类型来尝试找出它

    第一

    List.map allocCreater
    

    这已经不正确了,map 的第一个参数需要有 'a -&gt; 'b 类型但你已经给出了一个列表

    List.map List.tail matchCollection
    

    这是string list list

    我不知道你到底想在这里做什么,但你似乎缺少一个函数或有太多列表

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-22
      • 2014-03-01
      • 2012-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多