Loading ...
Sorry, an error occurred while loading the content.

Re: Counter in List.iter?

Expand Messages
  • darooha
    Here s how I would do it: let print_list words = List.fold_left (fun i w - Printf.printf %d: %s n i w; (i+1)) 1 words List.fold_left is pretty important, so
    Message 1 of 9 , Nov 21, 2012
    • 0 Attachment
      Here's how I would do it:

      let print_list words =
      List.fold_left (fun i w -> Printf.printf "%d: %s\n" i w; (i+1)) 1 words


      List.fold_left is pretty important, so it's definitely something you should learn.
      Also, Printf.printf is a useful tool.

      ---D. Sleator

      --- In ocaml_beginners@yahoogroups.com, "George" <siberianowl@...> wrote:
      >
      > I have a list with words. I need to print them and number them.
      > 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?
      >
    • Esther Baruk
      Yes, but in this case, the type returned by List.fold_left is int and the print_word function should definitely have unit return type. Esther Baruk ...
      Message 2 of 9 , Nov 21, 2012
      • 0 Attachment
        Yes, but in this case, the type returned by List.fold_left is int and the
        print_word function should definitely have unit return type.

        Esther Baruk


        On Wed, Nov 21, 2012 at 1:01 PM, darooha <sleator@...> wrote:

        > **
        >
        >
        > Here's how I would do it:
        >
        > let print_list words =
        > List.fold_left (fun i w -> Printf.printf "%d: %s\n" i w; (i+1)) 1 words
        >
        > List.fold_left is pretty important, so it's definitely something you
        > should learn.
        > Also, Printf.printf is a useful tool.
        >
        > ---D. Sleator
        >
        > --- In ocaml_beginners@yahoogroups.com, "George" <siberianowl@...> wrote:
        > >
        > > I have a list with words. I need to print them and number them.
        > > 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?
        > >
        >
        >
        >


        [Non-text portions of this message have been removed]
      • Minhyuk Kwon
        Hello I think, your problem is not good when using functional. You can make it functional. But, use reference! It s ok! We are not on the pure functional
        Message 3 of 9 , Nov 21, 2012
        • 0 Attachment
          Hello
          I think, your problem is not good when using functional. You can make
          it functional.

          But, use reference! It's ok!
          We are not on the pure functional world right?

          Minhyuk Kwon
          ---------------

          2012. 11. 21. 오후 9:45 Esther Baruk <esther.baruk@...> 작성:

          > Yes, but in this case, the type returned by List.fold_left is int and the
          > print_word function should definitely have unit return type.
          >
          > Esther Baruk
          >
          >
          > On Wed, Nov 21, 2012 at 1:01 PM, darooha <sleator@...> wrote:
          >
          >> **
          >>
          >>
          >> Here's how I would do it:
          >>
          >> let print_list words =
          >> List.fold_left (fun i w -> Printf.printf "%d: %s\n" i w; (i+1)) 1 words
          >>
          >> List.fold_left is pretty important, so it's definitely something you
          >> should learn.
          >> Also, Printf.printf is a useful tool.
          >>
          >> ---D. Sleator
          >>
          >> --- In ocaml_beginners@yahoogroups.com, "George" <siberianowl@...> wrote:
          >>>
          >>> I have a list with words. I need to print them and number them.
          >>> 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?
          >
          >
          > [Non-text portions of this message have been removed]
          >
          >
          >
          > ------------------------------------
          >
          > Archives up to December 31, 2011 are also downloadable at http://www.connettivo.net/cntprojects/ocaml_beginners
          > The archives of the very official ocaml list (the seniors' one) can be found at http://caml.inria.fr
          > Attachments are banned and you're asked to be polite, avoid flames etc.Yahoo! Groups Links
          >
          >
          >
        • Francois Berenger
          ... For your info, this line: i := !i+1 is incr i in the standard library (increment an integer reference). ... List.iteri (fun i w - Printf.printf %d: %s n
          Message 4 of 9 , Nov 21, 2012
          • 0 Attachment
            On 11/20/2012 10:20 PM, George wrote:
            > I have a list with words. I need to print them and number them.
            > 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

            For your info, this line:
            i := !i+1
            is
            incr i
            in the standard library (increment an integer reference).

            But otherwise, the code you are looking for is:

            ---
            List.iteri
            (fun i w -> Printf.printf "%d: %s\n" i w)
            words
            ---

            Or, a little shorter if you use the List and Printf modules a lot:

            ---
            module L = List
            module P = Printf

            L.iteri
            (fun i w -> P.printf "%d: %s\n" i w)
            words
            ---

            > ) words;;
            >
            > Is there a better way?
            >
            >
          • Florent Monnier
            ... In recent version of OCaml there s List.iteri, otherwise you can use List.fold_left instead: let _ = List.fold_left (fun i w - print_endline
            Message 5 of 9 , Nov 22, 2012
            • 0 Attachment
              2012/11/20, George <siberianowl@...>:
              > I have a list with words. I need to print them and number them.
              > 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?

              In recent version of OCaml there's List.iteri, otherwise you can use
              List.fold_left instead:

              let _ =
              List.fold_left (fun i w ->
              print_endline ((string_of_int i) ^ ": " ^ w);
              (i+1)
              ) 1 words;;

              --
              Cheers
            Your message has been successfully submitted and would be delivered to recipients shortly.