-3

I need to read a text file character by character in SMLNJ and store it in a list. The file consists of one line with numbers, without spaces or any form of separation. My question is how do I get a single character from the file and add it to the list of characters?

Example:

12345678

Result:

val input = [1, 2, 3, 4, 5, 6, 7, 8]
  • 2
    This isn't really a question. Are you having difficulty with some part of this? If so, which part? (Do you know how to read a text file character by character? Do you know how to assemble a list?) – ruakh May 03 '16 at 23:16
  • 1
    Possible duplicate of [How to get a string from TextIO in sml/ml?](http://stackoverflow.com/questions/14529807/how-to-get-a-string-from-textio-in-sml-ml) – John Coleman May 03 '16 at 23:18

1 Answers1

1

Using the following code you can get a list of characters by reading the contents of the file as a string (TextIO.vector to be accurate). The explode function is used for the conversion to a list of characters.

fun parse file =
let
    fun next_String input = (TextIO.inputAll input) 
    val stream = TextIO.openIn file
    val a = next_String stream
in
    explode(a)
end  
kostas
  • 26
  • 3