0
输入:只包含1与它SML/NJ:提取了许多的数字为一个int列表
输出整行的文本文件:用数字的位数
这里INT名单是我的代码:
fun parse file =
let
val input = TextIO.openIn file
fun read_digits (NONE,acc) = rev acc
| read_digits (SOME e,acc) =
let
val c = Option.valOf (e)
val str = Char.toString c
val digit = Option.valOf (Int.fromString str)
in
read_digits (TextIO.input1 input,digit::acc)
end
in
read_digits (TextIO.input1 input,nil: int list)
end
这里是我得到的错误信息:
test.sml:14.11-14.55 Error: operator and operand don't agree [tycon mismatch]
operator domain: char option option * int list
operand: TextIO.elem option * int list
in expression:
read_digits (TextIO.input1 input,digit :: acc)
test.sml:17.5-17.52 Error: operator and operand don't agree [tycon mismatch]
operator domain: char option option * int list
operand: TextIO.elem option * int list
in expression:
read_digits (TextIO.input1 input,nil: int list)
uncaught exception Error
raised at: ../compiler/TopLevel/interact/evalloop.sml:66.19-66.27
../compiler/TopLevel/interact/evalloop.sml:44.55
../compiler/TopLevel/interact/evalloop.sml:292.17-292.20
任何建议关于如何处理这个问题?
否则,没有人有更好的想法从一个文本文件中提取一个数字的数字,到一个int列表?
非常感谢您! :D 另外,你是对的,我应该检查新的行字符。我没有这么做的唯一原因是因为我知道该文件只包含一行,所以没有任何读'\ n'字符的风险。但是,最好是安全,而不是抱歉。 谢谢! – Orion
@Orion(1)例如'echo“12345”> digits.txt'会插入它(可以使用'-n'选项来避免); (2)你不需要文件后也应该关闭文件描述符 –
(1)你说得对,我用编辑器创建了这个文件,在这种情况下没有添加任何不必要的'\ n'。 (2)注意。再次感谢! :) – Orion