# Reading list dynamically for Prolog and Aleph

**URL:** <https://swi-prolog.discourse.group/t/reading-list-dynamically-for-prolog-and-aleph/5254>\
**Category:** Predicate\
**Created:** [April 10, 2022, 10:32am UTC](https://swi-prolog.discourse.group/t/reading-list-dynamically-for-prolog-and-aleph/5254 "2022-04-10T10:32:34Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![durgeshnandini](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/durgeshnandini/32/4339_2.png) [@durgeshnandini](https://swi-prolog.discourse.group/u/durgeshnandini)\
**Post date:** [April 10, 2022, 10:32am UTC](https://swi-prolog.discourse.group/t/reading-list-dynamically-for-prolog-and-aleph/5254/1 "2022-04-10T10:32:34Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![friguzzi](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/friguzzi/32/7045_2.png) [@friguzzi](https://swi-prolog.discourse.group/u/friguzzi)\
**Post date:** [April 11, 2022, 9:44am UTC](https://swi-prolog.discourse.group/t/reading-list-dynamically-for-prolog-and-aleph/5254/2 "2022-04-11T09:44:33Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![durgeshnandini](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/durgeshnandini/32/4339_2.png) [@durgeshnandini](https://swi-prolog.discourse.group/u/durgeshnandini)\
**Post date:** [April 11, 2022, 2:25pm UTC](https://swi-prolog.discourse.group/t/reading-list-dynamically-for-prolog-and-aleph/5254/3 "2022-04-11T14:25:49Z")

</div>

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]

---

<div class="post-metadata">

**Author:** ![friguzzi](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/friguzzi/32/7045_2.png) [@friguzzi](https://swi-prolog.discourse.group/u/friguzzi)\
**Post date:** [April 11, 2022, 3:32pm UTC](https://swi-prolog.discourse.group/t/reading-list-dynamically-for-prolog-and-aleph/5254/4 "2022-04-11T15:32:09Z")

</div>

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

```prolog
?- 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?

---

<div class="post-metadata">

**Author:** ![durgeshnandini](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/durgeshnandini/32/4339_2.png) [@durgeshnandini](https://swi-prolog.discourse.group/u/durgeshnandini)\
**Post date:** [April 11, 2022, 3:58pm UTC](https://swi-prolog.discourse.group/t/reading-list-dynamically-for-prolog-and-aleph/5254/5 "2022-04-11T15:58:38Z")

</div>

true in my query indicates that one item in the list\_A matched an item in list\_B

---

<div class="post-metadata">

**Author:** ![friguzzi](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/friguzzi/32/7045_2.png) [@friguzzi](https://swi-prolog.discourse.group/u/friguzzi)\
**Post date:** [April 11, 2022, 4:13pm UTC](https://swi-prolog.discourse.group/t/reading-list-dynamically-for-prolog-and-aleph/5254/6 "2022-04-11T16:13:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![durgeshnandini](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/durgeshnandini/32/4339_2.png) [@durgeshnandini](https://swi-prolog.discourse.group/u/durgeshnandini)\
**Post date:** [April 11, 2022, 4:34pm UTC](https://swi-prolog.discourse.group/t/reading-list-dynamically-for-prolog-and-aleph/5254/7 "2022-04-11T16:34:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![durgeshnandini](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/durgeshnandini/32/4339_2.png) [@durgeshnandini](https://swi-prolog.discourse.group/u/durgeshnandini)\
**Post date:** [April 12, 2022, 4:10pm UTC](https://swi-prolog.discourse.group/t/reading-list-dynamically-for-prolog-and-aleph/5254/8 "2022-04-12T16:10:23Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![friguzzi](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/friguzzi/32/7045_2.png) [@friguzzi](https://swi-prolog.discourse.group/u/friguzzi)\
**Post date:** [April 13, 2022, 6:32am UTC](https://swi-prolog.discourse.group/t/reading-list-dynamically-for-prolog-and-aleph/5254/9 "2022-04-13T06:32:35Z")

</div>

please show the code you are using

---

<div class="post-metadata">

**Author:** ![durgeshnandini](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/durgeshnandini/32/4339_2.png) [@durgeshnandini](https://swi-prolog.discourse.group/u/durgeshnandini)\
**Post date:** [April 13, 2022, 8:19pm UTC](https://swi-prolog.discourse.group/t/reading-list-dynamically-for-prolog-and-aleph/5254/10 "2022-04-13T20:19:04Z")

</div>

Here is the code below:

```prolog
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:

 ![Screenshot from 2022-04-13 22-18-24](https://global.discourse-cdn.com/free1/uploads/swiprolog/original/2X/4/497ca3383e45d3ffb6aa698de580036f5fdd426d.png)

---

<div class="post-metadata">

**Author:** ![friguzzi](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/friguzzi/32/7045_2.png) [@friguzzi](https://swi-prolog.discourse.group/u/friguzzi)\
**Post date:** [April 14, 2022, 9:58am UTC](https://swi-prolog.discourse.group/t/reading-list-dynamically-for-prolog-and-aleph/5254/11 "2022-04-14T09:58:38Z")

</div>

in the second and third clause common\_list has only two arguments

---

<div class="post-metadata">

**Author:** ![friguzzi](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/friguzzi/32/7045_2.png) [@friguzzi](https://swi-prolog.discourse.group/u/friguzzi)\
**Post date:** [April 14, 2022, 10:01am UTC](https://swi-prolog.discourse.group/t/reading-list-dynamically-for-prolog-and-aleph/5254/12 "2022-04-14T10:01:20Z")

</div>

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

> [@Useful Prolog references](https://swi-prolog.discourse.group/t/useful-prolog-references/1089):
>
> Excerpt: Are you new to Prolog? Do you need more information related to Prolog? Do you need a memory jog to get an idea related to SWI-Prolog out of the attic of your brain? Then you have come to the right place. Useful Prolog references Note: This is a topic for adding links to useful Prolog information for any one to find. If you have [Trust level](https://blog.discourse.org/2018/06/understanding-discourse-trust-levels/): [Basic](https://swi-prolog.discourse.group/badges/1/basic) you can add to this list by clicking on the edit icon in the lower right. [Capture] Note: Do not reply to this topic; questions, con…

and in particular the online book Learn Prolog Now
