Search code examples
smlmlpolyml

Write a SML function that take the name of a file and return a list of char without spaces


In an exam i found this exercise: "Write a function that take a file name (i.e. "text.txt") and return a list of char without blanks"

For example:

"text.txt" contains "ab e ad c" the function must return -> [#"a",#"b",#"e",#"a",#"d",#"c"]

Which is the easiest way to solve the exercise?

I've tried to use the library "TextIO" and the function "input1" but i got stuck. I don't know how to implement the function recursively. Could someone help?


Solution

  • fun chars filename =
      let
        val f = TextIO.openIn filename
        val s = TextIO.inputAll f
      in
        TextIO.closeIn f;
        List.filter (fn c => c <> #" ") (explode s)
      end