F#: how to print full list (Console.WriteLine() prints only first three elements)

f# printfn
list f#
fsharp print sequence
f# print line
fsharp print object
f# print int64
list rev f#
list filter f

Is it possible to print full list without using cycle? I tried:


and it prints only three first elements:

[1;2;3; ... ]

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):

> 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(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).

Console.WriteLine Method (System), C# Copy. public static void WriteLine (string format, object arg0, object arg1); (​invariant) (U) Universal full date/time: Sunday, June 27, 2004 3:11:04 AM (Y) Year: format items, that correspond to an object in the parameter list of this method. following output: // With the default new line characters: // // This is the first line. Aside from style, System.Console.WriteLine has the advantage of being able to reuse parameters, i.e. System.Console.WriteLine("This is a integer twice: {0} {0}", 5) Also, as noted here, you can do pretty printing of F# object using printfn which you can't do with System.Console.WriteLine and since it doesn't take a tuple, you can do partial application with it.

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.

In F# though it doesn't need to be done with a loop though but instead can be done with a nice pipe operation

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

Arrays in C# - working with arrays in CSharp, In this part of the C# programming tutorial, we will cover arrays. We will A scalar variable can hold only one item at a time. Number 1 is going to be the first element of the array, 2 the second. WriteLine(names[3]); Console. With the Join() method, we create one string from all elements of the array. 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#

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:

[<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 = ()

C#, C# language provides several techniques to read a collection of items. One of Because the foreach loop can only iterate any array or any collections which public static void Main() Array printing using for loop = GeeksforGeeks Array printing using foreach loop = Number of '1' = 3 Number of '4' = 3 Number of '3' = 3. Result: It is about 10 times faster to only call Console.WriteLine once. Combining strings before writing them is faster. Combining strings before writing them is faster. Tip: To run this benchmark, change the value of the "version" int from 0 to 1 (and back again).

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

[PDF] Introduction to programming and the C# language, 18. Print a book. 18. 3. Variables. 21. The sum of two numbers. 23. Operators. 24. 4 The first part is a brief introduction to programming and C#. The goal is to WriteLine(“Hello World”); Note that I did not show the whole code, but only the methods. The method Solve() solves the equation, and it performs three things:​. F# Checked Arithmetics Scope . F# allows to use checked arithmetics by opening Checked module, which redefines standard operators to be checked operators, for example: open Checked let x=1+System.Int32.MaxValue// overflow wil…

Formatted text using printf, Tips and techniques for printing and logging. Console.WriteLine("A string: {0}. An int: {1}. A float: {2}. A bool: {3}","hello",42,3.14,true) [1..5] |> List.map (sprintf "​i=%i") NET formatting functions only support using ToString() , but printf On the other hand, the “format strings” that are the first argument to  F#'s conversion functions are named by the type they convert to; akin to C casts. E.g., int 23.1 and int "23" both yield the integer 23. string is then the traditional “to string” method. Getting Started. The F# REPL and compiler are named fsi/fsc on Windows and fsharpi/fsharpc on Mac/Linux.

C# DateTime Format, First example. C# program that uses DateTime format using System; class Program { static void Format string pattern MMM display three-letter month ddd display three-letter day ToString("Y")); } } Output d 2/27/2009 D Friday, February 27, 2009 f Friday, WriteLine, we print all seven day strings we get from the dddd. List<T> stores elements of the specified type and it grows automatically. List<T> can store multiple null and duplicate elements. List<T> can be assigned to IList<T> or List<T> type of variable. It provides more helper method When assigned to List<T> variable ; List<T> can be access using indexer, for loop or foreach statement.

Introduction to Programming with C# / Java Books » Chapter 4 , Standard Input-Output; Printing to the Console; Console Input; Console Input and Output This happens because on the first line of code we use WriteLine(…) if we have formatting placeholder {4} and we submitted a list of only two arguments​. There are three main types of formatString components: double f = double. Method Example; Find(Predicate<T>) Finds a book by an ID using the IDToFind predicate delegate. C# example uses an anonymous delegate. FindAll(Predicate<T>) Find all books that whose Genre property is "Computer" using the FindComputer predicate delegate.

  • Maybe this answer helps you out -stackoverflow.com/questions/1508818/…
  • I noticed that "%A" with a seq<'a> will not print only a few elements (then ellipses). I used Seq.toList to convert it to a list.
  • %A doesn't print the whole list: printf "%A" [1 .. 500];; stops at 100.
  • @Clément I suspect this might have changed sometime in the last 7 years :-).
  • I don't think so :) AFAICT, it was already wrong back in 2009 (see e.g. stackoverflow.com/questions/1656200/… , which mentions the issue).
  • Looks like you're right. There's always list |> List.map (sprintf "%A") |> String.concat ", " |> sprintf "[%s]" then!
  • [1;2;3;4;5] |> Seq.iter (printf "%d ") -- w00t, currying :)
  • for x in [1;2;3;4;5] do printf "%d " x - I actually think simple for loop would be just as good as Seq.iter. It of course depends, but in some situations I personally prefer the straightforward (maybe more imperative?) solution.
  • If it's not possible, does it mean that Tomas Petricek's answer is incorrect?
  • @abatischev, @Tomas's answer is certainly correct and functional but under the hood a loop is occurring to print out the elements it's just not in the actual answer code.
  • @Juliet actually is not currying, is partial application :)
  • Interesting. Does the pretty-printer function need to be a public member?
  • @Joel -- it is unimportant because that property is obtained by refection API
  • Unfortunately, string concatenation is very inefficient operation on .NET (because it needs to copy the entire string), so this may have bad performance for large lists. In .NET, the recommended way would be to use StringBuilder (which is mutable and makes the solution a bit less functional).