Re: "ocaml_beginners":: Counter in List.iter?
- 2012/11/20, George <siberianowl@...>:
> I have a list with words. I need to print them and number them.In recent version of OCaml there's List.iteri, otherwise you can use
> The only way I found to do it is by using reference variables:
> let i = ref 1 in
> List.iter (fun w ->
> print_endline (string_of_int !i) ^ ": " ^ w;
> i := !i+1
> ) words;;
> Is there a better way?
let _ =
List.fold_left (fun i w ->
print_endline ((string_of_int i) ^ ": " ^ w);
) 1 words;;