如何在 SML 中获取整数作为用户输入
How to get an integer as user input in SML
I'm trying to get a number from a user. That number is then used to
call another function randomList(n)
which takes the given number and
uses it. I keep getting the error exception option
. I've read that
adding SOME to the variable declaration or valOf can fix this issue,
but it is not working for me. What do I need to change?
fun getNumber() =
print "Please enter the number of integers: ";
let
val str = valOf (TextIO.inputLine TextIO.stdIn)
val i : int = valOf (Int.fromString str)
in
randomList(i)
end;
getNumber();
这里的问题是 fun getNumber()
只包含下一行的 print
语句。如果您希望 print
和 let
都属于 getNumber()
.
,则需要将它们括在括号内
例如,以下代码编译并回显通过 stdin
传入的输入整数:
fun getNumber() = (
print "Please enter the number of integers: ";
let
val str = valOf (TextIO.inputLine TextIO.stdIn)
val i : int = valOf (Int.fromString str)
in
print(Int.toString(i))
end
);
getNumber();
I'm trying to get a number from a user. That number is then used to call another function
randomList(n)
which takes the given number and uses it. I keep getting the errorexception option
. I've read that adding SOME to the variable declaration or valOf can fix this issue, but it is not working for me. What do I need to change?
fun getNumber() =
print "Please enter the number of integers: ";
let
val str = valOf (TextIO.inputLine TextIO.stdIn)
val i : int = valOf (Int.fromString str)
in
randomList(i)
end;
getNumber();
这里的问题是 fun getNumber()
只包含下一行的 print
语句。如果您希望 print
和 let
都属于 getNumber()
.
例如,以下代码编译并回显通过 stdin
传入的输入整数:
fun getNumber() = (
print "Please enter the number of integers: ";
let
val str = valOf (TextIO.inputLine TextIO.stdIn)
val i : int = valOf (Int.fromString str)
in
print(Int.toString(i))
end
);
getNumber();