小编Hen*_*iro的帖子

在F#中切片/分组一系列相等的字符

我需要在文本中提取相等字符的序列.

例如:字符串"aaaBbbcccccccDaBBBzcc11211"应转换为字符串列表 ["aaa";"B";"bb";"ccccccc";"D";"a";"BBB";"z";"cc";"11";"2";"11"].

这是我的解决方案,直到现在:

let groupSequences (text:string) = 

    let toString chars =
        System.String(chars |> Array.ofList)

    let rec groupSequencesRecursive acc chars = seq {
        match (acc, chars) with
        | [], c :: rest -> 
            yield! groupSequencesRecursive [c] rest
        | _, c :: rest when acc.[0] <> c -> 
            yield (toString acc)
            yield! groupSequencesRecursive [c] rest
        | _, c :: rest when acc.[0] = c -> 
            yield! groupSequencesRecursive (c :: acc) rest
        | _, [] -> 
            yield (toString acc) …
Run Code Online (Sandbox Code Playgroud)

f# functional-programming

2
推荐指数
1
解决办法
255
查看次数

标签 统计

f# ×1

functional-programming ×1