Prolog member function should work but it doesnt

119 views Asked by At

I am trying to check if a string is a working call that can be executed. To do this I parse the string, get the first word, and if it matches a database of predefined functions, it should succeed. Q has the string, A will be used later, not now. Example of string is: append a and b.

is_uni(Q, A):-
    split_string(Q, " ", ",", [X|Y]),
    uni_db(Z),
    member(X, Z).

uni_db([
    append,
    member,
    append1
    ]).
2

There are 2 answers

0
coder On BEST ANSWER

You need to use atom_codes/2 predicate to convert stings to atoms, in example you need to convert "append" to append in order to work.

is_uni(Q,A):-
    split_string(Q, " ", ",", [X|Y]),
    atom_codes(W,X),
    uni_db(Z),
    member(W, Z).

Example:

?- is_uni("append a and b",A).
true ;
false.
1
coredump On
  1. You are confusing strings with atoms.

    "append" and 'append', a.k.a. append, are different. You can use atom_string/2 to convert between them:

    ..., atom_string(A, X), ...
    
  2. You are re-implementing built-in features.

    Why store commands in a list and iterate with member/2? Just define some facts:

    uni_db(append).
    uni_db(member).
    uni_db(append1).
    

    And then, you simply have to check whether uni_db(A). This is better supported by an implementation and more likely to be done efficiently.