Find repeating values in List

一曲冷凌霜 提交于 2020-01-26 01:46:06

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!