F #: creating collection of integers from user input
I'm new to F #, so the question might be pretty basic. However, I couldn't find any suggestions on SO.
I am playing with an algorithmic problem in F #. As a first step, I want to create a collection of integers from user console input. The number of entries is undefined. And I don't want to use any loops while
. I would prefer as much of the idiomatic approach as possible.
In a recursive function, I read the result and parse it with Int32.TryParse
. I am matching the bool result with match ... with
. If successful, I add the new value to the collection. Otherwise, I return the collection.
Below is my code:
let rec getNumList listSoFar =
let ok, num = Int32.TryParse(Console.ReadLine())
match ok with
| false -> listSoFar
| true -> getNumList num::listSoFar
let l = getNumList []
And the error I am getting:
Type mismatch. Waiting for '
but given' list
I know I am using types incorrectly, although I do not understand what exactly is wrong. Any explanation is much appreciated.
source to share