Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

read unknown number of lines in f#

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.

like image 726
qweqwe Avatar asked Jan 06 '15 09:01

qweqwe


1 Answers

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
like image 148
Tim Rogers Avatar answered Sep 30 '22 14:09

Tim Rogers