0

My function is trying to read a textfile line by line and carry out a certain predefined function on each line called somefn and append the value of somefn to the function. The somefn is already defined above this and works fine.

fun extractline(infile:string)=
    let
    val insl=TextIO.inputLine(ins)
    case insl of
    NONE=> []
    |SOME(l)=>somefn(insl)::extractline(infile)
    in
    TextIO.closeIn(ins);
    end
;

I am having errors and cannot handle them. I would appreciate some help.

Thank You.

700resu
  • 259
  • 6
  • 16

1 Answers1

1

Remember, in let ... in ... end blocks, you place declarations you need between let and i n, and the resulting expression between in and end.

As such, you need your case expression placed between in and end.

You also never open the stream, ins. Make your function open the stream, and then work recursively on that stream in another function, however; you don't want to open the file for each recursive call.

You'll want something on this form:

fun extractline file =
  let
      val ins = TextIO.openIn file

      fun extractline_h () = (* do something with ins here *)
  in
      extractline_h () before
      TextIO.closeIn ins
  end

Then you make extractline_h be recursive, and build up the list in that.

Sebastian Paaske Tørholm
  • 49,493
  • 11
  • 100
  • 118