(我知道this question,但它涉及到的序列,這是不是在這裏我的問題)分割基於謂詞列表到列表清單
鑑於此輸入(例如):
let testlist =
[
"*text1";
"*text2";
"text3";
"text4";
"*text5";
"*text6";
"*text7"
]
let pred (s:string) = s.StartsWith("*")
我想能夠調用MyFunc pred testlist
並得到如下的輸出:
[
["*text1";"*text2"];
["*text5";"*text6";"*text7"]
]
這是我目前的解決方案,但我真的不喜歡嵌套List.revs(忽視的事實是它需要SEQ作爲輸入)
let shunt pred sq =
let shunter (prevpick, acc) (pick, a) =
match pick, prevpick with
| (true, true) -> (true, (a :: (List.hd acc)) :: (List.tl acc))
| (false, _) -> (false, acc)
| (true, _) -> (true, [a] :: acc)
sq
|> Seq.map (fun a -> (pred a, a))
|> Seq.fold shunter (false, [])
|> snd
|> List.map List.rev
|> List.rev
遲來後記:這個問題的標題被嚴重措辭,於是幾個問題的答案都比較合適到[this](http://stackoverflow.com/questions/2279095/f-split-list-into-sublists-based-on-comparison-of-adjacent-elements)問題。 – Benjol 2013-02-05 05:46:31