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# - Fatal编程技术网

F# 是否使用其他列表筛选列表?

F# 是否使用其他列表筛选列表?,f#,F#,我有两份工作清单 type Item1 = { Id: int; .... } type Item2 = { Id: int; .... } let list1: Item1 list = .... let list2: Item2 list = .... 我需要筛选list1中存在Id的list2。以下代码不起作用 list1 |> Seq.filter (fun l1 -> (List.exists (fun l2 -> l1.Id = l2.Id) list2)) 它

我有两份工作清单

type Item1 = { Id: int; .... }
type Item2 = { Id: int; .... }
let list1: Item1 list = ....
let list2: Item2 list = ....
我需要筛选
list1
中存在
Id
list2
。以下代码不起作用

list1 |> Seq.filter (fun l1 -> (List.exists (fun l2 -> l1.Id = l2.Id) list2))
它希望列表2的类型为
Item1 list
。但是,下面的代码可以工作

list1 |> Seq.filter (fun l1 -> (list2 |> List.exists (fun l2 -> l1.Id = l2.Id) ))
为什么??它们不是用两种不同的方法编写具有相同功能的代码吗

F#型推理严格从左到右。因此,在这一部分中

 list1 |> Seq.filter (fun l1 -> (List.exists (fun l2 -> l1.Id = l2.Id) list2))
l2
的类型未知。因此,您会收到一条(有点奇怪的)错误消息

在第二种情况下,您有
list2 |>…
,这意味着当您进行比较时,
l2
的类型是已知的,因此一切都很好。

F#类型推断严格地从左到右。因此,在这一部分中

 list1 |> Seq.filter (fun l1 -> (List.exists (fun l2 -> l1.Id = l2.Id) list2))
l2
的类型未知。因此,您会收到一条(有点奇怪的)错误消息


在第二种情况下,您有
list2 |>…
,这意味着当您进行比较时,
l2
的类型是已知的,所以一切都很好。

有趣的是,这两种类型在F#3.0上对我来说都编译得很好,尽管我没有预料到它们会编译。有趣的是,这两个函数实际上都可以在F#3.0上编译,尽管我没有料到它们会编译。只有当是没有任何类型注释的函数的参数,并且该函数仍然没有在文件其余部分的任何位置使用时,如果
list2
的类型已知,但应该可以工作,则无法进行类型检查。只有当是一个没有任何类型注释的函数的参数,并且该函数仍然没有在文件的其余部分的任何地方使用时,才会无法进行类型检查