0

如果我在列表中找到重复值,我想返回 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]   

应该返回假。但我得到这个错误:

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

我应该怎么做才能修复错误?

4

1 回答 1

2

问题是

x::xs = 5::[]

在最后一种情况下

你想把它改成

|x::xs::xss when x=xs -> true
于 2013-10-01T03:35:20.990 回答