将字符列表(或数组)转换为字符串
问题描述:
如何将字符列表转换为字符串?
how does one convert from a list of characters to a string?
换句话说,如何反转List.ofSeq "abcd"
?
更新:new System.String (List.ofSeq "abcd" |> List.toArray) |> printfn "%A"
在有或没有new
的情况下似乎都能正常工作,但List.ofSeq "abcd" |> List.toArray) |> new System.String |> printfn "%A"
失败.为什么?
UPDATE: new System.String (List.ofSeq "abcd" |> List.toArray) |> printfn "%A"
seems to work fine, with or without new
, but List.ofSeq "abcd" |> List.toArray) |> new System.String |> printfn "%A"
fails. Why?
答
I asked a similar question once before. It seems object constructors aren't composable so you can't pass them as a function.
List.ofSeq "abcd" |> List.toArray |> (fun s -> System.String s) |> printfn "%A"
List.ofSeq "abcd" |> List.toArray |> (fun s -> new System.String(s)) |> printfn "%A"
更新 从F#4.0开始,构造函数是一流的函数.
Update Constructors are first-class functions as of F# 4.0
List.ofSeq "abcd" |> List.toArray |> System.String |> printfn "%A"