list F#:如何打印完整列表(Console.WriteLine() 仅打印前三个元素)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2519458/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
F#: how to print full list (Console.WriteLine() prints only first three elements)
提问by Nike
Is it possible to print full list without using cycle? I tried:
是否可以在不使用循环的情况下打印完整列表?我试过:
Console.WriteLine([1;2;3;4;5])
and it prints only three first elements:
它只打印三个第一个元素:
[1;2;3; ... ]
回答by Tomas Petricek
If you want to use the built-in F# formatting engine (and avoid implementing the same thing yourself), you can use F# printing functions such as printfn
. You can give it a format specifier to print an entire list (using F# formatting) or print just a first few elements (which happens when you call ToString
):
如果您想使用内置的 F# 格式化引擎(并避免自己实现同样的事情),您可以使用 F# 打印功能,例如printfn
. 你可以给它一个格式说明符来打印整个列表(使用 F# 格式)或只打印前几个元素(这在你调用 时发生ToString
):
> printfn "%A" [ 1 .. 5 ];; // Full list using F# formatting
[1; 2; 3; 4; 5]
> printfn "%O" [ 1 .. 5 ];; // Using ToString (same as WriteLine)
[1; 2; 3; ... ]
If you want to use Console.WriteLine
(or other .NET method) for some reason, you can also use sprintf
which behaves similarly to printf
, but returns the formatted string as the result:
如果您Console.WriteLine
出于某种原因想使用(或其他 .NET 方法),您也可以使用sprintf
which 的行为类似于printf
,但返回格式化的字符串作为结果:
Console.WriteLine(sprintf "%A" list)
The benefit of using printf
or sprintf
is that it also automatically deals with other F# types (for example if you have a list containing tuples, discriminated unions or records).
使用printf
or的好处sprintf
是它还可以自动处理其他 F# 类型(例如,如果您有一个包含元组、可区分联合或记录的列表)。
回答by JaredPar
No it's not possible to print the contents of an F# list without using a cycle / loop of sorts. To print every element you must enumerate each of them.
不,如果不使用各种循环/循环,就不可能打印 F# 列表的内容。要打印每个元素,您必须枚举它们中的每一个。
In F# though it doesn't need to be done with a loop though but instead can be done with a nice pipe operation
在 F# 中,虽然它不需要用循环来完成,但可以用一个很好的管道操作来完成
[1;2;3;4;5] |> Seq.iter (fun x -> printf "%d " x)
And as Juliet pointed out I could simplify this further with partial application
正如朱丽叶指出的那样,我可以通过部分应用进一步简化
[1;2;3;4;5] |> Seq.iter (printf "%d ")
回答by ssp
In general, if you want to change as a way an printf "%A" prints your objects as a way fsi.exe shows values fo your type, you can apply StructuredFormatDisplayAttribute attribute to your type:
通常,如果您想将 printf "%A" 打印对象的方式更改为 fsi.exe 显示您的类型值的方式,您可以将 StructuredFormatDisplayAttribute 属性应用于您的类型:
[<StructuredFormatDisplayAttribute("PP {PrettyPrinter}")>]
type Foo(a:string array) =
let pp = Array.mapi (fun i (s: string) -> sprintf "{idx: %d len: %d contents: '%s'}" i s.Length s) a
member x.PrettyPrinter = pp
> let foo = Foo [|"one";"two";"three"|];;
val foo : Foo =
PP [|"{idx: 0 len: 3 contents: 'one'}"; "{idx: 1 len: 3 contents: 'two'}";
"{idx: 2 len: 5 contents: 'three'}"|]
> printfn "%A" foo;;
PP [|"{idx: 0 len: 3 contents: 'one'}"; "{idx: 1 len: 3 contents: 'two'}";
"{idx: 2 len: 5 contents: 'three'}"|]
val it : unit = ()
回答by TwentyMiles
A perhaps more functional way of doing it:
一种可能更实用的方法:
let nums = [1;2;3;4;5;6]
let concat acc x = acc + " " + (string x)
let full_list = List.fold concat "" nums
printfn "%s" full_list