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

前端 未结 4 1446
梦谈多话
梦谈多话 2021-01-31 15:20

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:

4条回答
  •  爱一瞬间的悲伤
    2021-01-31 16:07

    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:

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

提交回复
热议问题