[英]F# : I need to format my output to only contain comma-separated integers, rather than the list format in F#
I have a function that takes a list of Integer values and returns the same.我有一个函数,它接受一个整数值列表并返回相同的值。 In the body of the function I need it to print out the output list before returning it.
在函数的主体中,我需要它在返回之前打印输出列表。 Currently it prints out the output below.
目前它打印出下面的输出。 I would like to print the items in the list out separated by commas without using any type of loop.
我想打印列表中的项目,以逗号分隔,而不使用任何类型的循环。
My output currently looks like: Even numbers : [8; 10; 12; 14]
我的输出目前看起来像:
Even numbers : [8; 10; 12; 14]
Even numbers : [8; 10; 12; 14]
My desired output formatting looks like: 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
Just map printf
over the list ( List.iter
is similar to List.map
in its functionality for this example).只是映射
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
Output:输出:
> evens_only [8; 10; 12; 14];;
Even numbers : 8 10 12 14
val it : List<Int32> = [8; 10; 12; 14]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.