F#中是否有多個實例模式?表達式測試順序中的所有項都是相同的
想想我正在做一個清單。我有以下模式匹配
match l with
| [] | [_] -> l //if the list is empty or contains only one item, simply return it
|
//is there a pattern to test if all of the elements are identical?
換句話說通過[]或[1]應該簡單地返回列表等應當[1; 1; 1; ...]但我無法弄清楚如何模式匹配最後的模式。這可能嗎?或者有更好的方法可以使用?我還沒有找到任何關於重複模式的任何地方。
@cfern,你說得對。我確實需要小心使用的術語。我傾向於使用「列表」和「序列」,就好像它們是同義詞 - 而在F#中它們絕對不是同義詞。我正在處理一個清單。 – 2009-10-29 12:00:42