【发布时间】:2016-03-14 20:22:21
【问题描述】:
我正在尝试在 sml 中创建堆栈,我尝试过使用列表;但我无法将元素添加到列表中。我正在尝试从输入文件中读取行,如果该行说:
push 5
push 9
add
quit
那么我希望输出文件是:
14
因为 5+9 是 14。 到目前为止,我已经能够创建布尔函数来识别该行是推送的还是具有数字的。
fun is_digit (c) = #"0" <= c andalso c <= #"9";
fun is_Push (c) = String.isSubstring "push" c;
fun stack(inFile : string, outFile : string) =
let
val ins = TextIO.openIn inFile;
val outs = TextIO.openOut outFile;
val readLine = TextIO.inputLine ins;
val it = []: string list;
fun helper(readLine : string option) =
case readLine of
NONE => ( TextIO.closeIn ins; TextIO.closeOut outs)
| SOME(c) => (
if is_Push c
then
let
val number = String.sub(c,5);
val numbChar = Char.toString number;
in
val myList = nil :: numbChar;
TextIO.output(outs, Int.toString(length myList))
end
else
TextIO.output(outs, "aaa\n");
helper(TextIO.inputLine ins))
in
helper(readLine)
end
【问题讨论】: