Recursion 在列表中查找重复的值
如果在列表中发现重复值,我希望返回trueRecursion 在列表中查找重复的值,recursion,f#,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 em
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
System.InvalidOperationException:输入列表为空。
在Microsoft.FSharp.Collections.FSharpList`1.get_Head()
在FSI_0003处。重复[a](FSharpList`1 L)
地址:$FSI_0004.main@()
在main@dm()
由于错误而停止
我应该怎么做才能修复错误?问题在于
x::xs = 5::[]
在最后一个案例中
你想把它改成
|x::xs::xss when x=xs -> true
编辑我的问题以匹配更改的代码。但是我仍然得到一个错误,删除第二个case块(
|x::xs when x=xs.Head->true
)@jth41-尽量不要编辑问题中的代码。最好保持原样,或者在之后添加一些内容,以明确代码已经更改