static Map lispMatch(Lisp pat, Lisp nl, Collection variables) { new Map matches; ret lispMatch_sub(pat, nl, variables, matches) ? matches : null; } static bool lispMatch_sub(Lisp pat, Lisp nl, Collection variables, Map m) { if (pat == null || nl == null) ret false; if (pat.isLeaf() && variables.contains(pat.head)) ret lispMatch_putMatch(m, pat.head, nl); if (neq(pat.head, nl.head)) ret false; // heads identical, proceed to children int n = pat.size(); if (n != nl.size()) ret false; for (int i = 0; i < n; i++) if (!lispMatch_sub(pat.get(i), nl.get(i), variables, m)) ret false; ret true; } static bool lispMatch_putMatch(Map matches, S key, Lisp val) { if (matches.containsKey(key)) { if (neq(matches.get(key), val)) false; //fail("multi-matching not implemented"); matches.put(key, val); } true; }