45

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; ... ]
1

4 Answers 4

67

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

5
  • 8
    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. Aug 5, 2014 at 16:11
  • 7
    %A doesn't print the whole list: printf "%A" [1 .. 500];; stops at 100.
    – Clément
    Jul 30, 2017 at 23:08
  • @Clément I suspect this might have changed sometime in the last 7 years :-). Jul 30, 2017 at 23:11
  • I don't think so :) AFAICT, it was already wrong back in 2009 (see e.g. stackoverflow.com/questions/1656200/… , which mentions the issue).
    – Clément
    Jul 30, 2017 at 23:14
  • 2
    Looks like you're right. There's always list |> List.map (sprintf "%A") |> String.concat ", " |> sprintf "[%s]" then! Jul 31, 2017 at 1:38
29

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 ")
5
  • 13
    [1;2;3;4;5] |> Seq.iter (printf "%d ") -- w00t, currying :)
    – Juliet
    Mar 25, 2010 at 21:22
  • 4
    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. Mar 25, 2010 at 21:37
  • 1
    If it's not possible, does it mean that Tomas Petricek's answer is incorrect? Mar 25, 2010 at 21:40
  • @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.
    – JaredPar
    Mar 25, 2010 at 21:49
  • 1
    @Juliet actually is not currying, is partial application :)
    – MattDavey
    Feb 25, 2013 at 14:57
8

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 = ()
2
  • Interesting. Does the pretty-printer function need to be a public member? Mar 26, 2010 at 6:31
  • @Joel -- it is unimportant because that property is obtained by refection API
    – ssp
    Mar 26, 2010 at 9:33
1

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
4
  • 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). Mar 25, 2010 at 21:34
  • Also, in recent versions of F#, List.fold_left is called List.fold and you can replace string_of_int with overloaded string function. Mar 25, 2010 at 21:35
  • Updated to match Tomas's comment. This language has been changing so fast lately, it's a little hard to keep up. Mar 25, 2010 at 21:40
  • Yes, especially the naming! However, now that F# is a part of Visual Studio 2010 (which is almost finished), there won't be that many changes (in the language and core libraries). Mar 25, 2010 at 21:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.