How to create an associative list in Reason?


#1

I am trying to figure out how to create an associative list but cannot find any documentation on it. When I try to create what I think is an assoc list in the reasonml playground I get a syntax error. An example of an assoc list looks like this:

[ 1, "a"; 2, "b"; 3, "c"]

Reference (search for getAssoc) : https://bucklescript.github.io/bucklescript/api/Belt_List.html


#2

I’m not an OCaml expert but it looks like the examples on that page are constructing a list of tuples, with syntax sugar that removes the parentheses from the tuples. Reason lists use commas, so maybe your list needs to look like

[(1, "a"),  (2, "b")]

and so on.

EDIT: the type of getAssoc begins with ('a * 'c) t which looks like a list of tuples to me!


#3

@crabmusket Yep, this is correct. Thanks! I will leave a complete example below for anyone else interested.

let map = [("black", 0)];

let b = "black";

let blackValue = Belt.List.getAssoc(map, b, (==));

Js.log(blackValue);