0

I am trying to write a method that when called, given a string list and the name of the output file, will output each element to the output file in SML. I have tried this, but it doesn't seem to work.

fun quit(outFile: string, list: string list) =
  let
    val outStream = TextIO.openOut outFile
    fun out(xs : string list) =  
          case xs of
              [] => (TextIO.closeOut outStream)
            | x::xs' => (TextIO.output(outStream, x); out(xs'))
  in
    out(list)
  end
ruakh
  • 175,680
  • 26
  • 273
  • 307
arizq29
  • 11
  • 5

1 Answers1

1

Your code works but it concatenates all of the strings together with no space between them. Presumably this is not what you want. If you want each item to be on a separate line then you will have to manually add the line breaks. On Windows you could do:

fun quit(outFile: string, list: string list) =
  let
    val outStream = TextIO.openOut outFile
    fun out(xs : string list) =  
          case xs of
              [] => (TextIO.closeOut outStream)
            | x::xs' => (TextIO.output(outStream, x ^ "\r\n"); out(xs'))
  in
    out(list)
  end;

(On Linux or Mac you would just use "\n").

Alternatively, if you want everything on a single line but with a delimiter such as a comma between the elements, you can first feed your list of strings to the function concatWith and then output a single (delimited) string to the text file.

John Coleman
  • 51,337
  • 7
  • 54
  • 119