1
votes

Playing with F# and trying to get from stringabcdefghijklmnop another string aeimbfjncgkodhlp.

let input ="abcdefghijklmnop"

let convertList (input:string)=
    input
    |> Seq.mapi(fun i ei->  i % 4, ei)
    |> Seq.groupBy (fst)
    |> Seq.map(fun (g,s)-> s |> Seq.map snd)

let result = convertList input

result

In my function final result is seq<seq<char>>, how to convert seq<seq<char>> to string?

1
Seq.concat, so just do result |> Seq.concat - Bent Tranberg
You might want to join us at F# Slack. For trivial questions like this one, join the beginners channel. - Bent Tranberg
@BentTranberg, you are right it helped me. Write answer i will accept it. - A191919

1 Answers

5
votes

In F#, the string type implements the seq<_> interface, which means that you can treat strings as sequences of characters, but getting the other way round is sadly a bit ugly.

One option is to turn seq<seq<char>> to just one seq<char> using Seq.concat, then convert it to seq<string> which is something you can then concatenate using String.concat:

result |> Seq.concat |> Seq.map string |> String.concat ""

Probably a more efficient, but less elegant option is to convert seq<char> to array<char> which you can then pass to System.String constructor:

result |> Seq.concat |> Seq.toArray |> System.String