Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
实现非常简单的F#函数的最佳方法_F# - Fatal编程技术网

实现非常简单的F#函数的最佳方法

实现非常简单的F#函数的最佳方法,f#,F#,我对F#和函数式编程非常陌生,我从今天开始学习!这是实现在字符串有数字时返回的函数的最佳方法吗 open System; let stringHasDigit (str: String) = not (String.forall(fun c -> (Char.IsDigit(c) = false)) str) printfn "%b" (stringHasDigit "This string has 1 digits") 由于string是Chars的序列,因此可以使用Se

我对F#和函数式编程非常陌生,我从今天开始学习!这是实现在字符串有数字时返回的函数的最佳方法吗

open System;

let stringHasDigit (str: String) = 
    not (String.forall(fun c -> (Char.IsDigit(c) = false)) str)

printfn "%b" (stringHasDigit "This string has 1 digits")

由于
string
Char
s的序列,因此可以使用
Seq
模块中的函数:

let hasDigits (s: string) =
    s |> Seq.exists Char.IsDigit

在查找
Seq
之前,先在
String
中查找函数。它们往往更快。这是使用
Seq.exists

let stringHasDigit (s: string) =
    String.exists Char.IsDigit s
顺便说一句,open语句末尾不需要分号