Reputation: 11
let downTo n =
if (n.Equals(0)) then
printfn "\n Empty List Provided \n";;
else
List.rev(List.init n (fun index -> index + 1));;
I'm trying to create a list 'n' length, if a number greater than 0 is passed. If not, print an error message. The List works by itself, but not in the If-Else statement. How can this be accomplished? Please help.
Upvotes: 1
Views: 488
Reputation: 19897
F# if
expressions are expressions not statements. What this means is that the two parts of the expression need to have the same type. In this case, one side has a type of int list
and the other side has a type of unit
.
Something like this would work for you better:
let downTo n =
if (n.Equals(0)) then
printfn "\n Empty List Provided \n"
[]
else
List.rev(List.init n (fun index -> index + 1))
or, alternatively, you could throw an exception but this has the drawback that you have to do a try/catch
around it.
Upvotes: 6