Artificial Intelligence: Implementing the Match Function in Clojure

Verified

Added on  2019/09/25

|2
|1228
|53
Homework Assignment
AI Summary
This assignment focuses on implementing a `match` function in Clojure, designed for pattern matching against arbitrarily nested lists. The function must handle patterns with and without keywords, returning an empty hash map if the pattern and subject are equal (without keywords), a hash map of keyword-value pairs if the pattern matches the subject, or nil if they do not match. The implementation requires a helper function, `matching`, which recursively compares the pattern and subject, managing a hash map to store keyword-value bindings. The assignment provides detailed examples and constraints, including the use of specific function and parameter names, and emphasizes understanding the underlying logic rather than relying on library functions. The solution showcases the implementation details, addressing base cases for keywords and recursive cases for sequences, demonstrating a clear understanding of the algorithm. The goal is to accurately determine if a pattern matches a subject based on the provided examples and specifications, ensuring correct behavior with and without keywords in the pattern.
Document Page
A pattern is an arbitrarily nested list. It can be matched against another arbitrarily nested list, called
its subject. The function match takes a pattern and a subject as its arguments. Suppose that the pattern
contains no keywords. If the pattern equals the subject, then match returns a true value: actually an
empty hash map, which Clojure writes as {}. If the pattern does not equal the subject,
then match returns a false value: actuallynil. In the following examples, the symbol ‘
means returns.
(match '() '()) {}
(match '() '(a b c)) nil
(match '(a b c) '(a b c)) {}
(match '(a (b c)) '(a (b c))) {}
(match '(a b) '(a (b))) nil
Things become more interesting if the pattern contains keywords. Then match may return a hash map,
whose keys are all the keywords from the pattern, and whose values are elements of the subject that
correspond to those keywords. If that happens, then we say that the pattern matches the subject. The
pattern matches the subject in all these examples.
(match '(:a) '(test)) {:a test}
(match '(:a is a :b) '(this is a test)) {:b test, :a this}
(match '(:a is a :b) '(this is a (hard test))) {:b (hard test), :a
this}
(match '(:a is :b) '((a rose) is (a rose))) {:b (a rose), :a (a
rose)}
(match '(:a is :a) '((a rose) is (a rose))) {:a (a rose)}
(match '(cons (first :a) (rest :b)) '(cons
(first x) (rest x)))
{:b x, :a x}
Clojure writes a hash map as a series of key-value pairs between { and }, separated by commas. The
order of the key-value pairs within the hash map does not matter. Also, if a keyword appears more than
once in a pattern, then it must match equal elements of the subject each time it appears. For example, the
keyword :a matches the nested list (a rose) twice.
Sometimes the keywords in the pattern do not correspond to elements of the subject, or the pattern has
elements that do not appear in the subject, etc. Then match returns nil, and we say that the pattern does
not match the subject. The pattern does not match the subject in all these examples.
(match '(:a) '()) nil
(match '(:a is a :b) '(this is not a test)) nil
(match '(:a is a :b) '(x is y)) nil
(match '(:a is :a) '((a rose) is (not a rose))) nil
(match '(cons (first :a) (rest :a)) '(cons (first x) (rest y))) nil
Again, if a keyword appears more than once in a pattern, then it must match equal elements of the subject
each time it appears. For example, the keyword :a cannot match both (a rose) and (not a rose),
so the pattern in the fourth example does not match.
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
. Implementation.
In this lab, you must write the function match in Clojure. It must behave in a way that is consistent with
the examples of the previous section. It is not enough if your match works only on these examples! You
must use the same names for functions and parameters that are given below: this will make your code
easier to grade. Here are some hints.
The function match must have two parameters, called pattern and subject.
If pattern matches subject, then match must return a hash map. Otherwise, match must
return nil.
The function match must immediately call a helper function, which must be called matching.
The function matching must take three arguments: a hash map called table, along
with pattern and subjectfrom match. When matching is called
from match, table must be an empty hash map.
The function matching must do all the work for match. The function match must never be
called again.
Think of matching as a generalization of the function equal-to? that was discussed in the
lectures. The two functions will be similar, although matching is a little more complex.
The function matching must have at least three cases.
One case (a base case) occurs when pattern is a keyword. If pattern has a value in table,
then test if its value equals subject. Use the test to decide whether to return table or nil.
If pattern does not have a value in table, then return a new hash map that is like table,
except that it associates pattern with subject.
Another case (the recursive case) occurs when pattern and subject are both
nonempty seq’s. It must call matching recursively with the first’s of both seq’s, and with
the rest’s of both seq’s.
The last case occurs when neither of the other cases occur. It must test
if pattern equals subject, then use the test to decide whether to return table or nil.
You may use as many additional helper functions as you need. However, whatever you write
must demonstrate that you understand how to write match. For example, if you use functions
from the Clojure library that let you write match without demonstrating that you understand it,
you will not get any points for this lab.
Don’t worry about tail recursion.
My Clojure code for match and matching are about one page long, including comments. If
you find yourself wanting to write pages and pages of code, then you have made mistakes.
chevron_up_icon
1 out of 2
circle_padding
hide_on_mobile
zoom_out_icon
[object Object]