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

前端 未结 4 1386
梦谈多话
梦谈多话 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:04

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

提交回复
热议问题