F#:我需要将我的输出格式化为仅包含以逗号分隔的整数,而不是 F# 中的列表格式

F# : I need to format my output to only contain comma-separated integers, rather than the list format in F#

我有一个接受整数值列表的函数,returns相同。在我的函数体中 需要它在返回之前打印出输出列表。目前它打印出下面的输出。我想使用任何类型的循环将列表中的项目打印出来,用逗号 分隔而不用

我的输出目前看起来像: Even numbers : [8; 10; 12; 14]

我的所需的输出格式如下所示: Even numbers : 8 10 12 14

let evens_only (x : List<Int32>) : List<Int32> =
    let output_list = x |> List.filter(fun y -> (y % 2) = 0)
    printfn "Even numbers : %A" output_list
    output_list

只需将 printf 映射到列表(List.iter 在本例中的功能类似于 List.map)。

let evens_only (x : List<Int32>) : List<Int32> =
    let output_list = x |> List.filter(fun y -> (y % 2) = 0)
    printf "Even numbers : "
    List.iter (fun i -> printf "%i " i) output_list
    printfn "" //extra newline
    output_list

输出:

> evens_only [8; 10; 12; 14];;
Even numbers : 8 10 12 14 
val it : List<Int32> = [8; 10; 12; 14]