Seq module

Reverse String
Reverse String in F#
module ReverseString

let reverse input =
    input
    |> Seq.rev
    |> Seq.toArray
    |> System.String

The string class implements the seq interface (which is an abbreviation of the CLI IEnumerable interface), which means we can use functions from the Seq module on it.

First, we pipe the input string into Seq.reverse, which returns an enumerable with the input in reverse order.

To convert the seq<char> returned by Seq.reverse back to a string, we first use Seq.toArray to convert it to a char[].

Finally, we convert the char array back to a string by piping it into the System.String constructor.

18th Dec 2024 · Found it useful?