Reading list dynamically for Prolog and Aleph

I am fairly new to Prolog and Aleph and need help with the following.

There are two lists: list_A and list_B, where list_A is a shorter list than list_B. For each item in list_A I want to check whether it is present in list_B. I have the following code for the same:

common_list(,).
common_list([X|Tail],Y):- member(X,Y).
common_list([X|Tail],Y):- common_list(Tail,Y).

But the problem is that I want to input list_A dynamically and want to define the elements of list_B from a database. For the output, I then want to list the elements that matched as a result.

So, for example,

  • list_B = [cho,pip,lib,exo,ram,lik,mangoes]
  • then I want that my program inputs a list from a sentence, say the sentence is ‘I like mangoes’ then the list should read list_A = [like,mangoes]
  • and then the output should be: true; mangoes.

So my main questions are:

  • how can I take the list_A dynamically as input?
  • how can I read list_B from a database?
  • how do I print the matching list elements?

Can you show examples of queries you would like to ask together with their answers?
For example

?- common_list([like,mangoes],[cho,pip,lib,exo,ram,lik,mangoes]).
true

Here is an example query and output:

? common_list[ i, like, mangoes], [list_B]).
true;
mangoes.

where list_B is defined in the program as

list_B = [cho,pip,lib,exo,ram,lik,mangoes]

A query instantiates a variable, so if you want your query to return mangoes you should add an argument to your predicate

?- ListB=[cho,pip,lib,exo,ram,lik,mangoes], common_list[ i, like, mangoes], List_B,C).
C=mangoes

What is the meaning of the true answer in your example?

true in my query indicates that one item in the list_A matched an item in list_B

Ah ok, I was confused by the ; besides true that I tought meant that mangoes was the second solution.
You can now try to modify your predicate common_list with the extra argument

oh yes, okay, sorry a genuine mistake from my side! I will modify with the solution you have suggested

The common_list has arity two (e.g. common_list([X],[X]).), when I add another arity, it gives an error.

How to fix the above error?

please show the code you are using

Here is the code below:

common_list([X],[X],C).
common_list([X|Tail],Y):- member(X,Y).
common_list([X|Tail],Y):- common_list(Tail,Y).

and here is the output:

in the second and third clause common_list has only two arguments

You seem to be a beginner in Prolog, you should look at

and in particular the online book Learn Prolog Now