Title: Pattern Matching a 2d array with tuples
Tags: ocaml
Question: I have a 2d list that looks as follows (string * int) list list
[ [(&quot;size1&quot;, 1);(&quot;size2&quot;,2)] ; [(&quot;size3&quot;,3);(&quot;size4&quot;,4)] ]
When given a string name, I want to return the int associated with that name
&quot;size1&quot; -&gt; 1
&quot;size2&quot; -&gt; 2
&quot;size4&quot; -&gt; 4
I have an idea of how I would do this using the List module
but how would I do something like this using pattern matching?
Comment: First try to do it for a 1D list and edit your post with your solution, then it'll be easier to generalize for a 2D list

Comment: The given syntax isn't an array. Arrays are typically written between `[|` and `|]`.

Comment: If these are `Lists` and not `Arrays`, then you can also take a look at [Association Lists](https://ocaml.org/api/List.html#1_Associationlists), because for some reason I can see them in the data provided, just that it looks like `List of Association Lists`.

Comment: You also likely want the function doing the lookup to return `int option` to allow for the possibility of not finding the key you provide. You could also throw an exception, but the option type is likely more idiomatic.


Here is another answer: An OCaml pattern essentially matches a single constructor of a type, with subpatterns in the places of the constructed values. For a list, this means you need to pick the number of 75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0 constructors you want to have in your pattern. Hence there's no OCaml pattern that matches at an arbitrary place in a list. So there's no way to solve your problem with just pattern matching (if I understand what you're trying to do).
If you know the maximum length of your lists you can enumerate all possible lengths, but somehow I doubt this is what you want to do.
My way of looking at it is that OCaml patterns have a very clean and disciplined structure, which allows them to be implemented very efficiently. Other languages (like perl say) have very powerful patterns that can't really be implemented efficiently. They can require more or less arbritrary amounts of retrying of different subpatterns. Naturally being an OCaml guy I much prefer the OCaml approach.
Comment for this answer: Ahh Okay, that makes sense thank you. I am still having trouble iterating through this list. I have tried using fold_left & fold_right but cant figure out how to return the value given the key
Comment for this answer: You need to make sure the function that runs on every iteration of the fold knows when the state has been found, and does not continue updating the state, instead letting the "found" state propagate out to the end of the fold.
Comment for this answer: This becomes very easy if you pass along an option type. `None` is a good indicator that you haven't found what you're looking for, while `Some ...` is.
Comment for this answer: The essence of folds is that you get to choose freely what state you carry along. If there is a final result that  you expect to find along the way, you want to make this part of the state. Then it's available at the end.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 4, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2022:00:03/07/22', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<ocaml>', 'Title': 'Pattern Matching a 2d array with tuples', 'ViewCount': 89, 'id': 71384683, 'user_AboutMe': None, 'user_AccountId': 23259852.0, 'user_DisplayName': 'kefkemfekm', 'user_Reputation': 33.0, 'user_Views': 4.0, 'user_id': 17346283.0}}