Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

42
LINES

< > BotCompany Repo | #1007704 // lispMatch

JavaX fragment (include)

sbool lispMatch_debug;

static Map<S, Lisp> lispMatch(Lisp pat, Lisp input, S... variables) {
  ret lispMatch(pat, input, asSet(variables));
}

static Map<S, Lisp> lispMatch(Lisp pat, Lisp input, Collection<S> variables) {
  new Map<S, Lisp> matches;
  ret lispMatch_sub(pat, input, variables, matches) ? matches : null;
}

static bool lispMatch_sub(Lisp pat, Lisp nl, Collection<S> variables, Map<S, Lisp> m) {
  if (pat == null || nl == null) ret false;
  if (pat.isLeaf() && variables.contains(pat.head)) {
    if (lispMatch_debug) print("Var: " + pat.head + " => " + nl);
    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_debug) print("Sub " + i + ": " + pat.get(i) + " => " + nl.get(i));
    if (!lispMatch_sub(pat.get(i), nl.get(i), variables, m))
      false;
  }
      
  true;
}

static bool lispMatch_putMatch(Map<S, Lisp> matches, S key, Lisp val) {
  if (matches.containsKey(key)) {
    if (neq(matches.get(key), val))
      false;
      //fail("multi-matching not implemented");
  } else
    matches.put(key, val);
  true;
}

Author comment

Began life as a copy of #1002842

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1007704
Snippet name: lispMatch
Eternal ID of this version: #1007704/9
Text MD5: 69913da24cc32766f46658055ea6587e
Author: stefan
Category: javax / a.i.
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-04-13 18:15:19
Source code size: 1276 bytes / 42 lines
Pitched / IR pitched: No / No
Views / Downloads: 654 / 601
Version history: 8 change(s)
Referenced in: #1006654 - Standard functions list 2 (LIVE, continuation of #761)
#1007821 - lispMatchIC - ignore case