External Predicates are C/C++ functions that can be called like predicates from ECLiPSe. Two following extra interface functions are provided for this purpose:
Apart from that, all functions for constructing, testing and decomposing ECLiPSe data can be used in writing the external predicate (see chapter 3). Here are two examples working with lists, the first one constructing a list in C:
#include "eclipse.h" int p_string_to_list() /* string_to_list(+String, -List) */ { pword list; char *s; long len; int res; res = ec_get_string_length(ec_arg(1), &s, &len); if (res != PSUCCEED) return res; list = ec_nil(); /* build the list backwards */ while (len--) list = ec_list(ec_long(s[len]), list); return ec_unify(ec_arg(2), list); }
The next example uses an input list of integers and sums up the numbers. It is written in C++:
#include "eclipseclass.h" extern "C" int p_sumlist() { int res; long x, sum = 0; EC_word list(EC_arg(1)); EC_word car,cdr; for ( ; list.is_list(car,cdr) == EC_succeed; list = cdr) { res = car.is_long(&x); if (res != EC_succeed) return res; sum += x; } res = list.is_nil(); if (res != EC_succeed) return res; return unify(EC_arg(2), EC_word(sum)); }
The source code of these examples can be found in directory doc/examples within the ECLiPSe installation.