Checking all the values in a matrix ocaml









up vote
1
down vote

favorite












I have this matrix:



let arr = Array.make_matrix 4 4 0;;


and what to check if all elements are 0.

I heard of the function for_all but I can't quite figure it out how to use it with a matrix, since it expects an int array or a int list.










share|improve this question

























    up vote
    1
    down vote

    favorite












    I have this matrix:



    let arr = Array.make_matrix 4 4 0;;


    and what to check if all elements are 0.

    I heard of the function for_all but I can't quite figure it out how to use it with a matrix, since it expects an int array or a int list.










    share|improve this question























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I have this matrix:



      let arr = Array.make_matrix 4 4 0;;


      and what to check if all elements are 0.

      I heard of the function for_all but I can't quite figure it out how to use it with a matrix, since it expects an int array or a int list.










      share|improve this question













      I have this matrix:



      let arr = Array.make_matrix 4 4 0;;


      and what to check if all elements are 0.

      I heard of the function for_all but I can't quite figure it out how to use it with a matrix, since it expects an int array or a int list.







      matrix ocaml






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 9 at 20:04









      Heitor Hellou

      62




      62






















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote













          According to the documentation (https://caml.inria.fr/pub/docs/manual-ocaml/libref/Array.html), here is everything you need to know:



          val for_all : ('a -> bool) -> 'a array -> bool


          Array.for_all p [|a1; ...; an|] checks if all elements of the array satisfy the predicate p. That is, it returns (p a1) && (p a2) && ... && (p an).



          Example: Array.for_all ((=) 0) has type int array -> bool and checks if all elements are zero.






          share|improve this answer



























            up vote
            0
            down vote













            A matrix is an array of arrays (or an array of rows if you prefer). So you need to execute a for_all on each one of the rows to check that all elements of the row are zero, and another outer for_all to check that all the for_alls over rows are true:



            let arr = Array.make_matrix 4 4 0 in
            Array.for_all (fun row ->
            Array.for_all ((=) 0) row) arr





            share|improve this answer




















              Your Answer






              StackExchange.ifUsing("editor", function ()
              StackExchange.using("externalEditor", function ()
              StackExchange.using("snippets", function ()
              StackExchange.snippets.init();
              );
              );
              , "code-snippets");

              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "1"
              ;
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function()
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled)
              StackExchange.using("snippets", function()
              createEditor();
              );

              else
              createEditor();

              );

              function createEditor()
              StackExchange.prepareEditor(
              heartbeatType: 'answer',
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader:
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              ,
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );













               

              draft saved


              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53232610%2fchecking-all-the-values-in-a-matrix-ocaml%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              1
              down vote













              According to the documentation (https://caml.inria.fr/pub/docs/manual-ocaml/libref/Array.html), here is everything you need to know:



              val for_all : ('a -> bool) -> 'a array -> bool


              Array.for_all p [|a1; ...; an|] checks if all elements of the array satisfy the predicate p. That is, it returns (p a1) && (p a2) && ... && (p an).



              Example: Array.for_all ((=) 0) has type int array -> bool and checks if all elements are zero.






              share|improve this answer
























                up vote
                1
                down vote













                According to the documentation (https://caml.inria.fr/pub/docs/manual-ocaml/libref/Array.html), here is everything you need to know:



                val for_all : ('a -> bool) -> 'a array -> bool


                Array.for_all p [|a1; ...; an|] checks if all elements of the array satisfy the predicate p. That is, it returns (p a1) && (p a2) && ... && (p an).



                Example: Array.for_all ((=) 0) has type int array -> bool and checks if all elements are zero.






                share|improve this answer






















                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  According to the documentation (https://caml.inria.fr/pub/docs/manual-ocaml/libref/Array.html), here is everything you need to know:



                  val for_all : ('a -> bool) -> 'a array -> bool


                  Array.for_all p [|a1; ...; an|] checks if all elements of the array satisfy the predicate p. That is, it returns (p a1) && (p a2) && ... && (p an).



                  Example: Array.for_all ((=) 0) has type int array -> bool and checks if all elements are zero.






                  share|improve this answer












                  According to the documentation (https://caml.inria.fr/pub/docs/manual-ocaml/libref/Array.html), here is everything you need to know:



                  val for_all : ('a -> bool) -> 'a array -> bool


                  Array.for_all p [|a1; ...; an|] checks if all elements of the array satisfy the predicate p. That is, it returns (p a1) && (p a2) && ... && (p an).



                  Example: Array.for_all ((=) 0) has type int array -> bool and checks if all elements are zero.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 9 at 21:03









                  de Vilhena

                  314




                  314






















                      up vote
                      0
                      down vote













                      A matrix is an array of arrays (or an array of rows if you prefer). So you need to execute a for_all on each one of the rows to check that all elements of the row are zero, and another outer for_all to check that all the for_alls over rows are true:



                      let arr = Array.make_matrix 4 4 0 in
                      Array.for_all (fun row ->
                      Array.for_all ((=) 0) row) arr





                      share|improve this answer
























                        up vote
                        0
                        down vote













                        A matrix is an array of arrays (or an array of rows if you prefer). So you need to execute a for_all on each one of the rows to check that all elements of the row are zero, and another outer for_all to check that all the for_alls over rows are true:



                        let arr = Array.make_matrix 4 4 0 in
                        Array.for_all (fun row ->
                        Array.for_all ((=) 0) row) arr





                        share|improve this answer






















                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          A matrix is an array of arrays (or an array of rows if you prefer). So you need to execute a for_all on each one of the rows to check that all elements of the row are zero, and another outer for_all to check that all the for_alls over rows are true:



                          let arr = Array.make_matrix 4 4 0 in
                          Array.for_all (fun row ->
                          Array.for_all ((=) 0) row) arr





                          share|improve this answer












                          A matrix is an array of arrays (or an array of rows if you prefer). So you need to execute a for_all on each one of the rows to check that all elements of the row are zero, and another outer for_all to check that all the for_alls over rows are true:



                          let arr = Array.make_matrix 4 4 0 in
                          Array.for_all (fun row ->
                          Array.for_all ((=) 0) row) arr






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 10 at 16:50









                          Anthony Scemama

                          944715




                          944715



























                               

                              draft saved


                              draft discarded















































                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53232610%2fchecking-all-the-values-in-a-matrix-ocaml%23new-answer', 'question_page');

                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Use pre created SQLite database for Android project in kotlin

                              Darth Vader #20

                              Ondo