Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I print an entire list in F#?

Tags:

f#

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?

like image 489
Jeff Avatar asked Nov 01 '09 02:11

Jeff


People also ask

How to print a list nicely Python?

If you just want to know the best way to print a list in Python, here's the short answer: Pass a list as an input to the print() function in Python. Use the asterisk operator * in front of the list to “unpack” the list into the print function. Use the sep argument to define how to separate two list elements visually.

How to print certain things from a list Python?

Use list slicing to print specific items in a list, e.g. print(my_list[1:3]) . The print() function will print the slice of the list starting at the specified index and going up to, but not including the stop index.

How to print a list on different lines Python?

Print list elements on separate Lines using sep # Use the sep argument to print the elements of a list on separate lines, e.g. print(*my_list, sep='\n') . The items in the list will get unpacked in the call to the print() function and will get printed on separate lines.


2 Answers

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 ""
like image 87
Brian Avatar answered Nov 08 '22 01:11

Brian


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

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

More info:

  • Lists in F# (MSDN)
like image 27
Christian C. Salvadó Avatar answered Nov 08 '22 01:11

Christian C. Salvadó