Merge/join seq of seqs

南楼画角 提交于 2019-12-05 19:34:44

This is essentially the same as your first solution, but a little more succinct:

let flatten l =
    seq {
        yield Seq.hd (Seq.hd l) (* first item of first list *)
        for a in l do yield! (Seq.skip 1 a) (* other items *)
    }

[Edit to add]:

If you need a List version of this code, use append |> Seq.to_list at the end of your method:

let flatten l =
    seq {
        yield Seq.hd (Seq.hd l) (* first item of first list *)
        for a in l do yield! (Seq.skip 1 a) (* other items *)
    } |> Seq.to_list
let merge = function
  | [] -> []
  | xs::xss -> xs @ [for _::xs in xss do yield! xs]

or:

let merge = function
  | [] -> []
  | xs::xss -> xs @ List.collect List.tail xss
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!