Say I have n
(7 in this case) inputs given
10
20
30
40
50
60
70
How do I read all inputs and store them in a list/array?
I tried this,
let inputList = [
while (let line = Console.ReadLine()) <> null do
line |> int
]
The idea was to read until I get an empty line.
But I get the following error,
Block following this 'let' is unfinished. Expect an expression.
To do this in a functional style, you can use Seq.initInfinite
to create a sequence from the console.
Then you need to terminate this list when you get a null value using Seq.takeWhile
. Beyond that, you can use all Seq
module functions available including Seq.toList
.
let read _ = Console.ReadLine()
let isValid = function null -> false | _ -> true
let inputList = Seq.initInfinite read |> Seq.takeWhile isValid |> Seq.toList
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With