问题
I want to return true if I find a repeating value in the list
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]
Should return false. But I get this error:
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
What should I do to fix the error?
回答1:
The problem is that
x::xs = 5::[]
in the last case
You want to change it to
|x::xs::xss when x=xs -> true
来源:https://stackoverflow.com/questions/19107482/find-repeating-values-in-list