Dictionary Look Up

/* lookup(X, Y, Z) is true if the entry X in the sorted tree Y has         */
/*    data Z.  Entries can be added, queried or verified.                  */
lookup(Key, dict(Key, Value, _, _), Value):-!.
lookup(Key, dict(Key1, _, Left, _), Value):-
  Key < Key1, !, lookup(Key, Left, Value).
lookup(Key, dict(Key1, _, _, Right), Value):-
  Key > Key1, lookup(Key, Right, Value).

test(X):-
      lookup("a", Dictionary, 1),  /* Add a = 1     */
      lookup("b", Dictionary, 2),  /* Add b = 2     */
      lookup("a", Dictionary, X),  /* Query a       */
   \+ lookup("a", Dictionary, 2),  /* Verify a != 2 */
      lookup("a", Dictionary, 1).  /* Verify a = 1  */

LPA Index     Home Page

Valid HTML 4.01 Strict