<noon>
I believe it was not tail-recursive because there was the try-with block around the recursive call
<Smerdyakov>
noon, yup, that's a restatement of what I just said. :)
<vixey>
like let rec lines input rest = try lines input (input_line input :: rest) with End_of_file -> close_input input; rest
<Smerdyakov>
noon, move the [try..with] to a context with no recursive calls, returning an [option] to indicate which case fired. That's the standard idiom.
<Smerdyakov>
vixey, that's not tail-recursive.
<Smerdyakov>
vixey, also, of course, your version returns different results than the original.
<Smerdyakov>
vixey, oops, never mind!
<vixey>
sorry I don't know ocaml as well as I thought ..I see why it's not tail recursive now
<vixey>
yes a function like input_line except, that returns option seems perfect
<cygnus_>
can you write map as a tail recursion?
<Smerdyakov>
cygnus_, every OCaml program can be written using no kind of recursion besides tail recursion.
<cygnus_>
why is the implementation of map not tail recursive
<noon>
... given sufficient work, in certain cases, probably
<Smerdyakov>
Search me.
<cygnus_>
google you? or you have no idea
<Smerdyakov>
noon, the "in certain cases" and "probably" are unnecessary.
<Smerdyakov>
cygnus_, no idea.
<Smerdyakov>
noon, there is an automatic transformation of every OCaml program to a version with only tail recursion.
<noon>
oh; it adds additional stacks?
<vixey>
are you talking about CPS or deforistation?
<Smerdyakov>
CPS translation is one possibility.
<Smerdyakov>
CPS-translated programs don't need stacks.
<noon>
Smerdyakov: I'll try to do my function without loops. Do you think it is a good idea to use a reference nevertheless? I don't feel too comfortable with mixing recursive functions and reference usage...
<Smerdyakov>
noon, no, I think you should think long and hard before using any imperative feature of OCaml. Your example doesn't pass my personal admission test.
<noon>
hehe
<noon>
Well, I have no idea on how to do that. It seems that there is no easy way to know when the stream is at end of file apart from catching the End_of_file exception. And in this case, how can I return a list that has been constructed by a recursive function inside the try block?
<Smerdyakov>
Like I said, catch the exception in a sub-expression with no recursive call.
<vixey>
..a function like input_line except, that returns option?
<noon>
Smerdyakov: okay, I have found, thank you!
willb has quit ["Leaving"]
<noon>
vixey: that's what I have done
<noon>
vixey: I hope it passes the admission test :)
<det>
Ocaml using exceptions instead of options is a PITA for recursive functions
<det>
match
<det>
try
<det>
Some (Gzip.input_char in_gzip)
<det>
with
<det>
End_of_file -> None
<det>
with
<det>
None ->
<det>
...
<det>
You have to do crap like that :<
* det
is afk now
<noon>
is det going to be kicked?
Anarchos has quit ["Vision[0.8.5-0418]: i've been blurred!"]