如何在f#中打印整个列表?

时间:2022-06-24 13:33:30

When I use Console.WriteLine to print a list, it defaults to only showing the first three elements. How do I get it to print the entire contents of the list?

当我使用控制台。若要打印列表,它默认只显示前三个元素。如何让它打印列表的全部内容?

2 个解决方案

#1


8  

You can use the %A format specifier along with printf to get a 'beautified' list printout, but like Console.WriteLine (which calls .ToString()) on the object, it will not necessarily show all the elements. To get them all, iterate over the whole list. The code below shows a few different alternatives.

您可以使用%A格式说明符和printf来获得一个“美化”的列表打印输出,但是类似于控制台。在对象上调用. tostring()),它并不一定显示所有的元素。要得到它们,请遍历整个列表。下面的代码显示了一些不同的替代方案。

let smallList = [1; 2; 3; 4]
printfn "%A" smallList // often useful

let bigList = [1..200]
printfn "%A" bigList // pretty, but not all

printfn "Another way"
for x in bigList do 
    printf "%d " x
printfn ""

printfn "Yet another way"
bigList |> List.iter (printf "%d ")
printfn ""

#2


6  

You can iterate over the it, using the List.iter function, and print each element:

您可以使用列表对it进行迭代。iter函数,打印每个元素:

let list = [1;2;3;4]
list |> List.iter (fun x -> printf "%d " x)

More info:

更多信息:

#1


8  

You can use the %A format specifier along with printf to get a 'beautified' list printout, but like Console.WriteLine (which calls .ToString()) on the object, it will not necessarily show all the elements. To get them all, iterate over the whole list. The code below shows a few different alternatives.

您可以使用%A格式说明符和printf来获得一个“美化”的列表打印输出,但是类似于控制台。在对象上调用. tostring()),它并不一定显示所有的元素。要得到它们,请遍历整个列表。下面的代码显示了一些不同的替代方案。

let smallList = [1; 2; 3; 4]
printfn "%A" smallList // often useful

let bigList = [1..200]
printfn "%A" bigList // pretty, but not all

printfn "Another way"
for x in bigList do 
    printf "%d " x
printfn ""

printfn "Yet another way"
bigList |> List.iter (printf "%d ")
printfn ""

#2


6  

You can iterate over the it, using the List.iter function, and print each element:

您可以使用列表对it进行迭代。iter函数,打印每个元素:

let list = [1;2;3;4]
list |> List.iter (fun x -> printf "%d " x)

More info:

更多信息: