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

102
LINES

< > BotCompany Repo | #1001192 // enter, ask, answer - a Lisp channeling exercise

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Libraryless. Click here for Pure Java version (873L/6K/21K).

1  
!752
2  
3  
static new L<Lisp> statements;
4  
5  
p {
6  
  init();
7  
  dialog();
8  
}
9  
10  
static Lisp lisplist(O... x) { return lisp("", x); }
11  
static Lisp enter(O... x) { return lisp("enter", x); }
12  
static Lisp answer(O... x) { return lisp("answer", x); }
13  
static Lisp or(O... x) { return lisp("or", x); }
14  
15  
static void init() {
16  
  Lisp statement = lisplist(
17  
    enter("An image has pixels."),
18  
    enter("What does an image have?"),
19  
    answer(or("pixels", "An image has pixels."))
20  
  );
21  
  statements.add(statement);
22  
  //print(structure(statement));
23  
  print(statement);
24  
}
25  
26  
static void dialog() {
27  
  Lisp list = lisplist();
28  
  while (true) {
29  
    System.out.print("LISP> ");
30  
    S s = readLine();
31  
    if (s == null) break;
32  
    s = s.trim();
33  
    if (s.equalsIgnoreCase("bye")) break;
34  
    if (s.equals("!new")) {
35  
      list = lisplist();
36  
      print("! ok");
37  
      continue;
38  
    }
39  
    /*if (s.startsWith("?")) {
40  
      s = s.substring(1).trim();
41  
      list.add(ask(s));
42  
      print("List now: " + list);
43  
    } else {*/
44  
      list.add(enter(s));
45  
      print("List now: " + list);
46  
    //}
47  
    
48  
    S a = chimeIn(list);
49  
    if (a != null) {
50  
      list.add(answer(a));
51  
      print("! " + a);
52  
    }
53  
  }
54  
}
55  
56  
static class Match {
57  
  Lisp statement;
58  
  int i;
59  
  
60  
  *(Lisp *statement, int *i) {}
61  
}
62  
63  
static S chimeIn(Lisp history) {
64  
  Match match = matchFromBeginning(history);
65  
  print("Match: " + structure(match));
66  
  if (match != null && match.i < match.statement.size()) {
67  
    Lisp next = match.statement.get(match.i);
68  
    if (next.is("answer"))
69  
      if (next.get(0).is("or"))
70  
        return next.get(0).getString(0);
71  
      else
72  
        return next.getString(0);
73  
  }
74  
  return null;
75  
}
76  
77  
static Match matchFromBeginning(Lisp history) {
78  
  Match best = null;
79  
  for (Lisp statement : statements) {
80  
    Match match = getMatch(statement, history);
81  
    if (best == null || match.i > best.i)
82  
      best = match;
83  
  }
84  
  return best;
85  
}
86  
87  
static Match getMatch(Lisp statement, Lisp history) {
88  
  int n = Math.min(statement.size(), history.size());
89  
  int i = 0;
90  
  while (i < n)
91  
    if (!matchLine(statement.get(i), history.get(i)))
92  
      break;
93  
    else
94  
      ++i;
95  
  return new Match(statement, i);
96  
}
97  
98  
static boolean matchLine(Lisp l1, Lisp l2) {
99  
  if (l1.is("enter"))
100  
    return l1.head.equals(l2.head) && sentenceEq(l1.getString(0), l2.getString(0));
101  
  return l1.equals(l2);
102  
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1001192
Snippet name: enter, ask, answer - a Lisp channeling exercise
Eternal ID of this version: #1001192/1
Text MD5: 41cf99a06ab6d71ca3212832df1111ab
Transpilation MD5: dbcb843b884d162342b9596c591f1757
Author: stefan
Category: logic code
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-02-14 23:42:41
Source code size: 2420 bytes / 102 lines
Pitched / IR pitched: No / No
Views / Downloads: 708 / 981
Referenced in: [show references]