Addition between int option and intAbusing the algebra of algebraic data types - why does this work?Data structure for keeping track of a game board in OCamlOCaml - What data type is some and none?how to write a function to find max number in a list of type number in ocaml?need to create a function builiding functionOcaml: add up all the integers in an int list and output it as an int OptionProper way of applying two (or many) option values to a function in F#OCaml compiler error “variant type list has no constructor true”Force a broader type for optional argument with more restrictive default valuePsum not accumulating (Polymorphic Higher order function) without forcing a type

Can an open source licence be revoked if it violates employer's IP?

Digital signature that is only verifiable by one specific person

Does WiFi affect the quality of images downloaded from the internet?

Is there a risk to write an invitation letter for a stranger to obtain a Czech (Schengen) visa?

For Saintsbury, which English novelists constituted the "great quartet of the mid-eighteenth century"?

Is there a term for someone whose preferred policies are a mix of Left and Right?

When is the phrase "j'ai bon" used?

Basic power tool set for Home repair and simple projects

How to search for Android apps without ads?

How can I detect if I'm in a subshell?

How can religions without a hell discourage evil-doing?

How to remove multiple elements from Set/Map AND knowing which ones were removed?

Arcane Tradition and Cost Efficiency: Learn spells on level-up, or learn them from scrolls/spellbooks?

Should I email my professor to clear up a (possibly very irrelevant) awkward misunderstanding?

Is it unethical to quit my job during company crisis?

100-doors puzzle

Why is gun control associated with the socially liberal Democratic party?

At zero velocity, is this object neither speeding up nor slowing down?

The title "Mord mit Aussicht" explained

Should I worry about having my credit pulled multiple times while car shopping?

How can Caller ID be faked?

Sci fi/fantasy book, people stranded on a planet where tech doesn't work, magic mist

How did the European Union reach the figure of 3% as a maximum allowed deficit?

Fastest path on a snakes and ladders board



Addition between int option and int


Abusing the algebra of algebraic data types - why does this work?Data structure for keeping track of a game board in OCamlOCaml - What data type is some and none?how to write a function to find max number in a list of type number in ocaml?need to create a function builiding functionOcaml: add up all the integers in an int list and output it as an int OptionProper way of applying two (or many) option values to a function in F#OCaml compiler error “variant type list has no constructor true”Force a broader type for optional argument with more restrictive default valuePsum not accumulating (Polymorphic Higher order function) without forcing a type






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








2















I have an integer matrix of n rows by m cols representing a game board, and have written two functions that allow me to retrieve and set values within the matrix.



let get_val board (row, col) = 
if row < 0
|| col < 0
|| (Array.length data) < (row + 1)
|| (Array.length data.(row)) < (col + 1)
then None
else if (board.(row).(col)) = (-1)
then None
else Some (board.(row).(col)) ;;

let set_val board (row, col) i =
if row < 0
|| col < 0
|| (Array.length data) < row+1
|| (Array.length data.(row)) < col+1
|| i < 0
then invalid_arg "set: invalid arguments"
else board.(row).(col) <- i;
board ;;

let board = Array.make_matrix 4 4 ;;


All positions are set to -1 initially to represent an empty square. Basically, if I try to retrieve a value outside of the board, I get a None. If the location is valid and not an empty square, I get can retrieve the value at that matrix as a Some type. I would like to increment a position by 1 in a board by using these two functions.



My first attempt in the board by 1 by doing the following:



let board = set_val board (2, 2) ((get_val board (2, 2)) + 1)


However, I run into the type issue,



This expression has type int option but an expression was expected of type int 


, which I understand is because "get_val" returns a Some type, and 1 is an integer. I also tried:



let board = set_val board (2, 2) ((get_val board (2, 2)) + (Some 1))


, but board is an integer matrix. The constraints of the problem require me to return a Some/None from "get_val", but I need a way to retrieve the values from the function "get" as an int, not a Some. I've looked into how to convert from an Option to an int, but came up with nothing, since I'm not allowed access to the Option module. I suspect I'm not looking up the correct thing, but have run out of places to look. How would I be able to use the result of "get_val" in a way that I can increment the new value for a position on the board?










share|improve this question




























    2















    I have an integer matrix of n rows by m cols representing a game board, and have written two functions that allow me to retrieve and set values within the matrix.



    let get_val board (row, col) = 
    if row < 0
    || col < 0
    || (Array.length data) < (row + 1)
    || (Array.length data.(row)) < (col + 1)
    then None
    else if (board.(row).(col)) = (-1)
    then None
    else Some (board.(row).(col)) ;;

    let set_val board (row, col) i =
    if row < 0
    || col < 0
    || (Array.length data) < row+1
    || (Array.length data.(row)) < col+1
    || i < 0
    then invalid_arg "set: invalid arguments"
    else board.(row).(col) <- i;
    board ;;

    let board = Array.make_matrix 4 4 ;;


    All positions are set to -1 initially to represent an empty square. Basically, if I try to retrieve a value outside of the board, I get a None. If the location is valid and not an empty square, I get can retrieve the value at that matrix as a Some type. I would like to increment a position by 1 in a board by using these two functions.



    My first attempt in the board by 1 by doing the following:



    let board = set_val board (2, 2) ((get_val board (2, 2)) + 1)


    However, I run into the type issue,



    This expression has type int option but an expression was expected of type int 


    , which I understand is because "get_val" returns a Some type, and 1 is an integer. I also tried:



    let board = set_val board (2, 2) ((get_val board (2, 2)) + (Some 1))


    , but board is an integer matrix. The constraints of the problem require me to return a Some/None from "get_val", but I need a way to retrieve the values from the function "get" as an int, not a Some. I've looked into how to convert from an Option to an int, but came up with nothing, since I'm not allowed access to the Option module. I suspect I'm not looking up the correct thing, but have run out of places to look. How would I be able to use the result of "get_val" in a way that I can increment the new value for a position on the board?










    share|improve this question
























      2












      2








      2








      I have an integer matrix of n rows by m cols representing a game board, and have written two functions that allow me to retrieve and set values within the matrix.



      let get_val board (row, col) = 
      if row < 0
      || col < 0
      || (Array.length data) < (row + 1)
      || (Array.length data.(row)) < (col + 1)
      then None
      else if (board.(row).(col)) = (-1)
      then None
      else Some (board.(row).(col)) ;;

      let set_val board (row, col) i =
      if row < 0
      || col < 0
      || (Array.length data) < row+1
      || (Array.length data.(row)) < col+1
      || i < 0
      then invalid_arg "set: invalid arguments"
      else board.(row).(col) <- i;
      board ;;

      let board = Array.make_matrix 4 4 ;;


      All positions are set to -1 initially to represent an empty square. Basically, if I try to retrieve a value outside of the board, I get a None. If the location is valid and not an empty square, I get can retrieve the value at that matrix as a Some type. I would like to increment a position by 1 in a board by using these two functions.



      My first attempt in the board by 1 by doing the following:



      let board = set_val board (2, 2) ((get_val board (2, 2)) + 1)


      However, I run into the type issue,



      This expression has type int option but an expression was expected of type int 


      , which I understand is because "get_val" returns a Some type, and 1 is an integer. I also tried:



      let board = set_val board (2, 2) ((get_val board (2, 2)) + (Some 1))


      , but board is an integer matrix. The constraints of the problem require me to return a Some/None from "get_val", but I need a way to retrieve the values from the function "get" as an int, not a Some. I've looked into how to convert from an Option to an int, but came up with nothing, since I'm not allowed access to the Option module. I suspect I'm not looking up the correct thing, but have run out of places to look. How would I be able to use the result of "get_val" in a way that I can increment the new value for a position on the board?










      share|improve this question














      I have an integer matrix of n rows by m cols representing a game board, and have written two functions that allow me to retrieve and set values within the matrix.



      let get_val board (row, col) = 
      if row < 0
      || col < 0
      || (Array.length data) < (row + 1)
      || (Array.length data.(row)) < (col + 1)
      then None
      else if (board.(row).(col)) = (-1)
      then None
      else Some (board.(row).(col)) ;;

      let set_val board (row, col) i =
      if row < 0
      || col < 0
      || (Array.length data) < row+1
      || (Array.length data.(row)) < col+1
      || i < 0
      then invalid_arg "set: invalid arguments"
      else board.(row).(col) <- i;
      board ;;

      let board = Array.make_matrix 4 4 ;;


      All positions are set to -1 initially to represent an empty square. Basically, if I try to retrieve a value outside of the board, I get a None. If the location is valid and not an empty square, I get can retrieve the value at that matrix as a Some type. I would like to increment a position by 1 in a board by using these two functions.



      My first attempt in the board by 1 by doing the following:



      let board = set_val board (2, 2) ((get_val board (2, 2)) + 1)


      However, I run into the type issue,



      This expression has type int option but an expression was expected of type int 


      , which I understand is because "get_val" returns a Some type, and 1 is an integer. I also tried:



      let board = set_val board (2, 2) ((get_val board (2, 2)) + (Some 1))


      , but board is an integer matrix. The constraints of the problem require me to return a Some/None from "get_val", but I need a way to retrieve the values from the function "get" as an int, not a Some. I've looked into how to convert from an Option to an int, but came up with nothing, since I'm not allowed access to the Option module. I suspect I'm not looking up the correct thing, but have run out of places to look. How would I be able to use the result of "get_val" in a way that I can increment the new value for a position on the board?







      ocaml






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 25 at 2:23









      Alex PenizAlex Peniz

      205




      205






















          1 Answer
          1






          active

          oldest

          votes


















          1














          The best/idiomatic Ocaml way to do this is using pattern matching.



          let board = match (S.get grid (row, col)) with 
          | None -> S.set grid (row, col) 1
          | Some v -> S.set grid (row, col) (v+1)


          Apparently, this way you can strip the Some part of the v and just get the actual value.






          share|improve this answer




















          • 3





            It would be cleaner to store an int option in the board instead of misusing -1 as None. But if the board is gigantic then memory requirement might make that necessary.

            – Goswin von Brederlow
            Mar 25 at 12:31







          • 1





            Naps and shower are the best way to think about problems.

            – Richard-Degenne
            Mar 26 at 12:58











          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',
          autoActivateHeartbeat: false,
          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%2f55330524%2faddition-between-int-option-and-int%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          The best/idiomatic Ocaml way to do this is using pattern matching.



          let board = match (S.get grid (row, col)) with 
          | None -> S.set grid (row, col) 1
          | Some v -> S.set grid (row, col) (v+1)


          Apparently, this way you can strip the Some part of the v and just get the actual value.






          share|improve this answer




















          • 3





            It would be cleaner to store an int option in the board instead of misusing -1 as None. But if the board is gigantic then memory requirement might make that necessary.

            – Goswin von Brederlow
            Mar 25 at 12:31







          • 1





            Naps and shower are the best way to think about problems.

            – Richard-Degenne
            Mar 26 at 12:58















          1














          The best/idiomatic Ocaml way to do this is using pattern matching.



          let board = match (S.get grid (row, col)) with 
          | None -> S.set grid (row, col) 1
          | Some v -> S.set grid (row, col) (v+1)


          Apparently, this way you can strip the Some part of the v and just get the actual value.






          share|improve this answer




















          • 3





            It would be cleaner to store an int option in the board instead of misusing -1 as None. But if the board is gigantic then memory requirement might make that necessary.

            – Goswin von Brederlow
            Mar 25 at 12:31







          • 1





            Naps and shower are the best way to think about problems.

            – Richard-Degenne
            Mar 26 at 12:58













          1












          1








          1







          The best/idiomatic Ocaml way to do this is using pattern matching.



          let board = match (S.get grid (row, col)) with 
          | None -> S.set grid (row, col) 1
          | Some v -> S.set grid (row, col) (v+1)


          Apparently, this way you can strip the Some part of the v and just get the actual value.






          share|improve this answer















          The best/idiomatic Ocaml way to do this is using pattern matching.



          let board = match (S.get grid (row, col)) with 
          | None -> S.set grid (row, col) 1
          | Some v -> S.set grid (row, col) (v+1)


          Apparently, this way you can strip the Some part of the v and just get the actual value.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 27 at 10:54









          Sir E_net4 the Downvoter

          13.7k74077




          13.7k74077










          answered Mar 25 at 3:11









          Alex PenizAlex Peniz

          205




          205







          • 3





            It would be cleaner to store an int option in the board instead of misusing -1 as None. But if the board is gigantic then memory requirement might make that necessary.

            – Goswin von Brederlow
            Mar 25 at 12:31







          • 1





            Naps and shower are the best way to think about problems.

            – Richard-Degenne
            Mar 26 at 12:58












          • 3





            It would be cleaner to store an int option in the board instead of misusing -1 as None. But if the board is gigantic then memory requirement might make that necessary.

            – Goswin von Brederlow
            Mar 25 at 12:31







          • 1





            Naps and shower are the best way to think about problems.

            – Richard-Degenne
            Mar 26 at 12:58







          3




          3





          It would be cleaner to store an int option in the board instead of misusing -1 as None. But if the board is gigantic then memory requirement might make that necessary.

          – Goswin von Brederlow
          Mar 25 at 12:31






          It would be cleaner to store an int option in the board instead of misusing -1 as None. But if the board is gigantic then memory requirement might make that necessary.

          – Goswin von Brederlow
          Mar 25 at 12:31





          1




          1





          Naps and shower are the best way to think about problems.

          – Richard-Degenne
          Mar 26 at 12:58





          Naps and shower are the best way to think about problems.

          – Richard-Degenne
          Mar 26 at 12:58



















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55330524%2faddition-between-int-option-and-int%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

          Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

          Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

          Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript