如何在F#中打印整个列表? [英] How do I print an entire list in F#?

查看:73
本文介绍了如何在F#中打印整个列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用Console.WriteLine打印列表时,默认情况下仅显示前三个元素.如何获取列表的全部内容?

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?

推荐答案

您可以将%A格式说明符与printf一起使用,以获取美化"列表打印输出,但类似于Console.WriteLine(调用.ToString())在对象上,它不一定会显示所有元素.要获得全部信息,请遍历整个列表.下面的代码显示了几种不同的选择.

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.

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 ""

这篇关于如何在F#中打印整个列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆