问题
I'm trying to read text from a file in SML. Eventually, I want a list of individual words; however, I'm struggling at how to convert between a TextIO.elem
to a string
. For example, if I write the following code it returns a TextIO.elem
but I don't know how to convert it to a string so that I can concat it with another string
TextIO.input1 inStream
回答1:
TextIO.elem
is just a synonym for char
, so you can use the str
function to convert it to a string. But as I replied to elsewhere, I suggest using TextIO.inputAll
to get a string right away.
Here is a function that takes an instream and delivers all (remaining) words in it:
val words = String.tokens Char.isSpace o TextIO.inputAll
The type of this function is TextIO.instream -> string list
.
来源:https://stackoverflow.com/questions/14529807/how-to-get-a-string-from-textio-in-sml-ml