2013-10-01 59 views
0

重複值,我想,如果我在列表查找列表

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返回true。但我得到這個錯誤:

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>[email protected]() 
    at [email protected]() 
Stopped due to error 

我該怎麼辦才能解決這個錯誤?

回答

3

的問題是,

x::xs = 5::[] 
在最後一種情況下

你想將其更改爲

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

編輯我的問題,以匹配改變的代碼。但我仍然得到一個錯誤 – jth41

+0

刪除第二個大小寫塊(x = xs.Head - > true'時的'| x :: xs) – khyperia

+0

Bingo。很好的工作現在很thx – jth41