F# Split list into sublists based on comparison of adjacent elements

后端 未结 6 798
日久生厌
日久生厌 2020-12-03 17:46

I\'ve found this question on hubFS, but that handles a splitting criteria based on individual elements. I\'d like to split based on a comparison of adjacent elements, so the

6条回答
  •  旧时难觅i
    2020-12-03 18:23

    How about:

    let splitOn test lst =
        List.foldBack (fun el lst ->
                match lst with
                | [] -> [[el]]
                | (x::xs)::ys when not (test el x) -> (el::(x::xs))::ys
                | _ -> [el]::lst
             )  lst [] 
    

    the foldBack removes the need to reverse the list.

提交回复
热议问题