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

Re: "ocaml_beginners"::[] Counter in List.iter?

Expand Messages
  • Sebastien Mondet
    iteri does what you want: List.iteri ;; - : (int - a - unit) - a list - unit = ... [Non-text portions of this message have been removed]
    Message 1 of 9 , Nov 20, 2012
    • 0 Attachment
      'iteri' does what you want:

      List.iteri ;;
      - : (int -> 'a -> unit) -> 'a list -> unit = <fun>





      On Tue, Nov 20, 2012 at 8:20 AM, 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?
      >
      >
      >
      > ------------------------------------
      >
      > 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
      >
      >
      >
      >


      [Non-text portions of this message have been removed]
    • Esther Baruk
      Hi, The latest version of OCaml includes new stdlib functions like List.iteri or List.mapi which integrate a counter in their arguments. List.iteri has the
      Message 2 of 9 , Nov 20, 2012
      • 0 Attachment
        Hi,

        The latest version of OCaml includes new stdlib functions like List.iteri
        or List.mapi which integrate a counter in their arguments.

        List.iteri has the following signature: (int -> 'a -> unit) -> 'a list ->
        unit

        Cheers,

        Esther Baruk


        On Tue, Nov 20, 2012 at 2:20 PM, 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]
      • rixed@happyleptic.org
        If your ocaml stdlib s do not have List.iteri you can find it on batteries : http://ocaml-batteries-team.github.com/batteries-included/hdoc/BatList.html
        Message 3 of 9 , Nov 20, 2012
        • 0 Attachment
          If your ocaml stdlib's do not have List.iteri you can find it
          on batteries : http://ocaml-batteries-team.github.com/batteries-included/hdoc/BatList.html
        • 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 4 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 5 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 6 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 7 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 8 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.