Splitting a list into list of lists based on predicate

前端 未结 5 2125
感动是毒
感动是毒 2021-01-21 03:28

(I am aware of this question, but it relates to sequences, which is not my problem here)

Given this input (for example):

let testlist = 
    [  
       \         


        
5条回答
  •  误落风尘
    2021-01-21 04:09

    yet another one...

    let partition pred lst = 
        let rec trec xs cont =
            match xs with
            | []               -> ([],[]) |> cont
            | h::t when pred h -> (fun (y,n) -> h::y,n) >> cont |> trec t
            | h::t             -> (fun (y,n) -> y,h::n) >> cont |> trec t
        trec lst id
    

    then we can define shunt:

    let shunt pred lst = lst |> partition pred |> (fun (x,y) -> [x;y])
    

提交回复
热议问题