Question: 10. Write a function check_pat that takes a pattern and returns true if and only if all the variables appearing in the pattern are distinct

10. Write a function check_pat that takes a pattern and returns true if and only if all the variables appearing in the pattern are distinct from each other (i.e., use different strings). The constructor names are not relevant. Hints: The sample solution uses two helper functions. The first takes a pattern and returns a list of all the strings it uses for variables. Using List.fold_left with a function that uses @ is useful in one case. The second takes a list of strings and decides if it has repeats. List.exists or List.mem may be useful. Sample solution is 15 lines. These are hints: We are not requiring List.fold_left and List.exists/List.mem here, but they make it easier.

11. 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.

12. Write a function first_match of type valu -> pattern list -> (string * valu) list option. It returns None if no pattern in the list matches or Some lst where lst is the list of bindings for the first pattern in the list that matches. Hints: Use first_answer and a try-with-expression. Sample solution is about 3 lines.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!