Here are some general hints:
- Name your variables something meaningful, like
filename
,lines
andline
. - The function
TextIO.inputLine
takes as argument a value of typeinstream
. When you write
TextIO.inputLine hd(ls)
, what this is actually interpreted as is(TextIO.inputLine hd) ls
, which means "treathd
as if it were aninstream
and try and read a line from it, take that line and treat it as if it were a function, and apply it onls
", which is of course complete nonsense.The proper parenthesising in this case would be
TextIO.inputLine (hd ls)
, which still does not make sense, since we decided thatls
is astring list
, and sohd ls
will be astring
and not aninstream
.
Here is something that resembles what you want to do, but opposite:
(* Open a file, read each line from file and return those that contain mySubstr *)
fun test (filename, mySubstr) =
let val instr = TextIO.openIn filename
fun loop () = case TextIO.inputLine instr of
SOME line => if String.isSubstring mySubstr line
then line :: loop () else loop ()
| NONE => []
val lines = loop ()
val _ = TextIO.closeIn instr
in lines end
You need to use TextIO.openOut
and TextIO.output
instead. TextIO.inputLine
is one that reads from files.