将函数应用于每个元素以在 F# 中使用 yield 生成序列

Apply function to each element to generate sequence with yield in F#

我是 F# 的新手,对我来说比较复杂的事情之一是如何正确使用 yield。 我正在尝试通过读取文本文件并为每一行提取带有 a 的值并将它们添加到序列中(结果)来生成序列(结果)

regexp 函数 来自 http://www.fssnip.net/29/title/Regular-expression-active-pattern

let (|Regex|_|) pattern input =
    let m = Regex.Match(input, pattern)
    if m.Success then Some(List.tail [ for g in m.Groups -> g.Value ])
    else None

let extractCoordinates input =
    match input with
    | Regex @"\(([0-9]{3})\)[-. ]?([0-9]{3})[-. ]?([0-9]{4})" [ area; prefix; suffix ] ->
        [ area; prefix; suffix ]
    | _ -> []

读取文件并生成序列

open System.IO

let filepath = __SOURCE_DIRECTORY__ + @"../../test_input_01.txt"

let values =
        File.ReadAllLines 
        |> Seq.map (fun l -> extractCoordinates l)

但它不起作用

error FS0001: The type 'string -> string []' is not compatible with the type 'seq<'a>'

谁能告诉我如何将函数应用于列表的每个元素并将输出存储到序列结果中?使用或不使用 yield...

您对 Seq.map 的处理方法是正确的。您得到的错误是因为没有向 File.ReadAllLines:

提供参数
let values =
    File.ReadAllLines filepath
    |> Seq.map (fun l -> extractCoordinates l)