prolog like operation in erlang ?

Kent Boortz kent@REDACTED
Fri Apr 25 03:26:17 CEST 2003


HP Wei <hp@REDACTED> writes:
> Suppose I define a relation through the following function:
> 
> rel(x1, m1, m2) -> true;
> rel(x2, m1, m2) -> true;
> rel(x3, m3, m6) -> true;
> ....
> 
> In prolog, if I am not mistaken,
> something like rel(X, m1, m2) can give me X = [x1, x2].

It has been a long time since I wrote Prolog but to get
a result like this in Prolog you have to use something like 
findall/setof/bagof.

> In erlang, how do I in general get the corresponding X ??
> Or should I encode the relation in a structure in this case ?

Erlang being functional you can't get a result like that without
splitting up the relation to functions with the mappings you want to
use. You may be able to simulate what you want using an ets table,
something like

  test() ->
      T = ets:new(foo,[duplicate_bag]),
      ets:insert(T, {x1, m1, m2}),
      ets:insert(T, {x2, m1, m2}),
      ets:insert(T, {x3, m3, m6}),
      X = ets:select(T, [{{'$1',m1, m2},[],['$1']}]).

but the order of the result will be undefined,

kent (did I just write my first match specification? ;-)



More information about the erlang-questions mailing list