F#有自己的字符串操作库吗?

Sco*_*rod 5 dll f#

F#有自己的字符串操作库吗?

当我试图学习F#时,我发现自己使用现有的System.string方法?

我应该这样做吗?

码:

open System

type PhoneNumber = 
    { CountryCode:int
      Number:string }

// b. Create a function formatPhone that accepts a PhoneNumber record and formats it to look like something like this: "+44 1234 456789"  
let formatPhone phoneNumber =

    let getLeadingCharacters (length:int) (text:string) =
        text.Substring(0, length)

    let getLastCharacters (length:int) (text:string) =
        text.Substring(text.Length - length, length)

    printf "+%i %s %s" phoneNumber.CountryCode 
                       (phoneNumber.Number |> getLeadingCharacters 4)
                       (phoneNumber.Number |> getLastCharacters 6)

formatPhone { CountryCode=44; Number="123456789" };;
Run Code Online (Sandbox Code Playgroud)

UPDATE

更新的功能来自:

let formatPhone phoneNumber =

    let getLeadingCharacters (length:int) (text:string) =
        text.Substring(0, length)

    let getLastCharacters (length:int) (text:string) =
        text.Substring(text.Length - length, length)

    printf "+%i %s %s" phoneNumber.CountryCode 
                       (phoneNumber.Number |> getLeadingCharacters 4)
                       (phoneNumber.Number |> getLastCharacters 6)

formatPhone { CountryCode=44; Number="123456789" };;
Run Code Online (Sandbox Code Playgroud)

至:

let formatPhone phoneNumber =

    printf "+%i %s %s" phoneNumber.CountryCode 
                       phoneNumber.Number.[0..3]
                       phoneNumber.Number.[4..8]

formatPhone { CountryCode=44; Number="123456789" };;
Run Code Online (Sandbox Code Playgroud)

Guy*_*der 8

不,F#没有特定的String库来复制.NET库.它有一个带有额外字符串函数的字符串模块.

是的,使用.NET函数.

F#可以使用所有.NET库的事实是它最强大的功能之一.使用curried参数将函数与使用元组参数的.NET函数混合起来似乎很奇怪.

这就是为什么在NuGet中你会看到也具有FSharp扩展名的包.例如MathNet NumericsMathNet Numerics FSharp.这些是包装函数,允许使用.NET库的惯用F#.

在寻找与F#一起使用的函数和方法时,我经常使用这个技巧.要搜索.NET class作为关键字使用并搜索F#特定代码,请使用module关键字.

例如:

Google:MSDN string class
第一项:字符串类

Google:MSDN string module
第一项:Core.String模块(F#)

  • 我通过关注[F#热门答案](http://stackoverflow.com/tags/f%23/topusers)并阅读大部分新问题来获取我的大部分知识.我在[CiteSeer](http://citeseer.ist.psu.edu/index)上阅读了很多研究论文,并编写了大量的代码.过去几个月我一直在研究神经网络的基础知识并坚持解决问题,即使他们需要一周或更长的时间才能解决问题.我最近了解到,调试神经网络的最佳方法之一是使用可视图像,而不是查看所有权重和偏差的值. (2认同)