On Thu, 29 Sep 2011, David Allsopp wrote:

I think it's probably something to do with optional arguments not
behaving as you expect.

uhm, would be interesting to see what is wrong on my use of the optional
arguments.

But this is a bad use for optional arguments - you should instead use
a nested function to pass the accumulator values. This works fine:

yes my first attempt was implemented with nested functions but ,,,

let enumerate l =
 let rec enumerate acc n = function
   h::ls -> enumerate ((n, h)::acc) (n + 1) ls
 | [] -> List.rev acc
 in
   enumerate [] 0 l

... my nested function had a different name than the nextee function and
I was passing l to it too. Could you explain me why your code works? in
particular where does it take the list to enumerate? Silly question I
know but it seems magic to me written in such a way

Concatenating lists is also expensive in terms of the left list so
your function is about as slow as possible! Much better when aiming
for tail recursion, accumulate a reversed list and then reverse it in
the basis case.

Ok, thanks for the note, but efficiency issues are still far in my
learning curve ;-)

Walter

--

--
Caml-list mailing list.  Subscription management and archives:
https://sympa-roc.inria.fr/wws/info/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs

Reply via email to