在List中查找重复值

时间:2013-10-01 03:33:29

标签: recursion f#

如果我在列表中找到重复值

,我想返回true
let rec repeats L = 
   match L with
   | [] -> false
   | x::xs when x = xs.Head -> true
   | x::xs -> repeats xs;;


repeats [1;2;3;4;5]   

应该返回false。但是我得到了这个错误:

System.InvalidOperationException: The input list was empty.
   at Microsoft.FSharp.Collections.FSharpList`1.get_Head()
   at FSI_0003.repeats[a](FSharpList`1 L)
   at <StartupCode$FSI_0004>.$FSI_0004.main@()
   at main@dm()
Stopped due to error

我该怎么做才能解决错误?

1 个答案:

答案 0 :(得分:2)

问题在于

x::xs = 5::[]

在最后一个案例中

您想将其更改为

|x::xs::xss when x=xs -> true
相关问题