F# - Display full results in F# interactive window

眉间皱痕 提交于 2019-11-30 02:37:42

问题


Disclaimer: Total F# Newbie question!

If I type the following into an F# file in Visual Studio

#light

let squares =
    seq { for x in 1 .. 10 -> x * x }

printf "%A" squares

and run F# interactive on it by highlighting and pressing Alt+Enter, the output in the interactive window is

> 
seq [1; 4; 9; 16; ...]
val squares : seq<int>

>

But I want to see the full sequence i.e.

> 
seq [1; 4; 9; 16; 25; 36; 49; 64; 81; 100]
val squares : seq<int>

>

Is this possible? I'm hoping that there is a setting for this that I've missed.


回答1:


'seq' is a lazily-evaluated construct; it could be infinite, which is why FSI only shows the first few values. If you want to see it all, an easy thing to do is convert to a list, e.g.

printf "%A" (squares |> Seq.tolist)



回答2:


If you want to display all the values in the sequence without transforming into a List, you can iterate directly on the sequence like so:

Seq.iter (printfn "%A") squares

Note that you're taking a risk: if, as Brian hints, the sequence is infinite, you could be in for a rather long wait. (In this case, Seq.skip and Seq.take are your friends)




回答3:


An alternative is to set fsi.PrintLength to a suitably large number, e.g.

> fsi.PrintLength <- 500


来源:https://stackoverflow.com/questions/1508818/f-display-full-results-in-f-interactive-window

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!