我正在尝试编写一个函数,该函数使用正向组合返回包含所有大写字符的字符串。
这是我的代码,没有前向组合:
let toUpper s = String.map System.Char.ToUpper s
Run Code Online (Sandbox Code Playgroud)
这是我尝试使用正向合成的方法:
let toUpper2 s = s >> Seq.map System.Char.ToUpper >> Seq.map string >> String.concat ""
Run Code Online (Sandbox Code Playgroud)
我使它与管道正向工作,但无法使其与正向合成一起工作。任何帮助表示赞赏!
This two are equivalent:
let toUpper1 = Seq.map System.Char.ToUpper >> Seq.map string >> String.concat ""
let toUpper2 s = s |> Seq.map System.Char.ToUpper |> Seq.map string |> String.concat ""
Run Code Online (Sandbox Code Playgroud)
但是出现问题toUpper1。它是通用的,会导致ML语言出现问题:
typecheck:值限制。当'_a:> seq时,推断值'toUpper1'具有通用类型val toUpper1:('_a->字符串)要么显式说明'toUpper1'的参数,要么,如果您不打算将其设为通用,添加类型注释。
所以它需要一个类型注释:
let toUpper1 : string -> string = Seq.map System.Char.ToUpper >> Seq.map string >> String.concat ""
Run Code Online (Sandbox Code Playgroud)