在列表中查找重复值 [英] Find repeating values in List

查看:73
本文介绍了在列表中查找重复值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在列表中找到重复的值,我想返回true

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]   

应返回false.但是我得到这个错误:

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

该如何解决该错误?

推荐答案

问题是

x::xs = 5::[]

在最后一种情况下

您要将其更改为

|x::xs::xss when x=xs -> true

这篇关于在列表中查找重复值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆