Question: Please help asap with this functions thank you so much In OCAML LANGUAGE 1. Write a function matches of type valu pattern (string valu) list
Please help asap with this functions thank you so much
In OCAML LANGUAGE



1. Write a function matches of type valu pattern (string valu) list option. It should take a value and a pattern, and return Some lst where lst is the list of bindings if the value matches the pattern, or None otherwise. Note that if the value matches but the pattern has no variables in it (i.e., no patterns of the form VariableP s), then the result is Some []. Hints: Sample solution has one match expression with 7 branches. The branch for tuples uses all_answers and List.combine. Sample solution is about 18 lines. Remember to look above for the rules for what patterns match what values, and what bindings they produce. - WildcardP matches everything and produces the empty list of bindings. - VariableP s matches any value v and produces the one-element list holding (s,v). - UnitP matches only Unit and produces the empty list of bindings. - ConstantP 17 matches only Constant 17 and produces the empty list of bindings (and similarly for other integers). - ConstructorP (s1,p) matches Constructor (s2,v) if s1 and s2 are the same string (you can compare them with =) and p matches v. The list of bindings produced is the list from the nested pattern match. We call the strings s1 and s2 the constructor name. Note that unlike in OCaml, we allow any string to be a constructor name. 2 - TupleP ps matches a value of the form Tuple vs if ps and vs have the same length and for all i, the ith element of ps matches the ith element of vs. The list of bindings produced is all the lists from the nested pattern matches appended together. - Nothing else matches. type pattern = WildcardP | VariableP of string | UnitP I ConstantP of int | ConstructorP of string pattern | TupleP of pattern list type valu = Constant of int | Unit | Constructor of string valu | Tuple of valu list
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
