Only match matches with fixed patterns, not variables. The corresponding use of correspondence in this case will look like this: (pay attention to the inclusion of "default", as in the other answer)
let list = [ "a", 1; "b", 2; "c", 3; "d", 4 ] let rec map_left_to_right e rules default = match rules with [] -> default (* No rules left to match *) | (first,second)::rest -> (* At least one rule remaining, which we'll put into first,second *) if first = e then second else map_left_to_right e rest default
If we want to return 0, if nothing is found, then it will be called like this:
map_left_to_right "b" list 0
All this is functionally equivalent to the code in another answer, and in practice I would recommend using this code, because it is smaller and uses existing libraries better, but I thought I would give this code because it better illustrates how it will be applied in this case matching mapping.
source share