1

I'm trying to read two integers which are going to be taken as input from the same line. My attempt so far:

let separator: char = 
    ' '
Console.ReadLine().Split separator
|> Array.map Convert.ToInt32

But this returns a two-element array and I have to access the individual indices to access the integers. Ideally, what I would like is the following:

let (a, b) =
    Console.ReadLine().Split separator
    |> Array.map Convert.ToInt32
    |> (some magic to convert the two element array to a tuple)

How can I do that?

Robur_131
  • 674
  • 1
  • 5
  • 16
  • Assuming you don't want to hardcode the number of integers, you might be able to adapt [How can i convert between F# List and F# Tuple?](https://stackoverflow.com/q/2920094/3744182) to your needs. – dbc Dec 06 '20 at 17:09
  • If you use [F#+](https://github.com/fsprojects/FSharpPlus) you can do `let (a, b) = sscanf "%i %i" (Console.ReadLine ())` – Gus Dec 06 '20 at 23:09

1 Answers1

2

I'm afraid there's no magic. You have to explicitly convert into a tuple

let a, b =
    Console.ReadLine().Split separator
    |> Array.map int
    |> (fun arr -> arr.[0], arr.[1])

Edit: you can use reflection as @dbc suggested but that's slow and probably overkill for what you're doing.

Koenig Lear
  • 2,366
  • 1
  • 14
  • 29