# How to call a predicate from another predicate

**URL:** <https://swi-prolog.discourse.group/t/how-to-call-a-predicate-from-another-predicate/541>\
**Category:** Help!\
**Created:** [April 14, 2019, 7:33pm UTC](https://swi-prolog.discourse.group/t/how-to-call-a-predicate-from-another-predicate/541 "2019-04-14T19:33:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Danilo](https://avatars.discourse-cdn.com/v4/letter/d/b3f665/32.png) [@Danilo](https://swi-prolog.discourse.group/u/Danilo)\
**Post date:** [April 14, 2019, 7:33pm UTC](https://swi-prolog.discourse.group/t/how-to-call-a-predicate-from-another-predicate/541/1 "2019-04-14T19:33:41Z")

</div>

Hi friends,

I have these predicates:

deleteall(N,[],[]).  
deleteall(N,[N|T],U):-!,deleteall(N,T,U).  
deleteall(N,[H|T],[H|U]):-deleteall(N,T,U).  
count(N,[],0).  
count(N,[N|T],U):-!,count(N,T,U1), U is U1+1.  
count(N,[H|T],U):-count(N,T,U).  
oc([],[]).  
oc([H|T],[[H,X]|Y]):-count(H,[H|T],X), deleteall(H,T,T1), oc(T1,Y).

start:-  
write(‘Type your list:’),  
read(L1),  
**call(oc([L1],N)).**

How can I read the values from keyboard using the “read” command and pass them to the “oc” predicative?

I really appreciate any help.  
🙂

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [April 14, 2019, 8:53pm UTC](https://swi-prolog.discourse.group/t/how-to-call-a-predicate-from-another-predicate/541/2 "2019-04-14T20:53:17Z")

</div>

Welcome to the SWI-Prolog forum. If you double post a question you should provide a link to the other [posting](https://stackoverflow.com/q/55679316/1243762).

If you have more questions please ask. 🙂

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [April 14, 2019, 10:07pm UTC](https://swi-prolog.discourse.group/t/how-to-call-a-predicate-from-another-predicate/541/3 "2019-04-14T22:07:39Z")

</div>

Using the same code I posted on [StackOverflow](https://stackoverflow.com/a/55679695/1243762)

Here is your code with the reading of a list of numbers from user input and running the code.

Code:

```prolog
read_list(Items) :-
    write('Please enter number, enter "Done" when finished: ' ),
    read_string(user, "\n", "\r", _, Response),
    (
        Response == "Done"
    ->
        Items = []
    ;
        valid_input(Response,Items)
    ).

valid_input(Response,Items) :-
    (
        number_string(Item,Response)
    ->
        % Item was valid
        % so get next number and
        % add to list on backtracking
        read_list(Items0),
        Items = [Item|Items0]
    ;
        % Item was invalid
        % so warn user of invalid number and what they input
        % and get next number.
        % Do not add invalid Item to list on backtracking.
        format('Invalid number: `~w''~n',[Response]),
        read_list(Items0),
        Items = Items0
    ).

deleteall(_N,[],[]).
deleteall(N,[N|T],U) :-
    !,
    deleteall(N,T,U).
deleteall(N,[H|T],[H|U]) :-
    deleteall(N,T,U).

count(_N,[],0).
count(N,[N|T],U) :-
    !,
    count(N,T,U1),
    U is U1 + 1.
count(N,[_H|T],U) :-
    count(N,T,U).

oc([],[]).
oc([H|T],[[H,X]|Y]) :-
    count(H,[H|T],X),
    deleteall(H,T,T1),
    oc(T1,Y).
    
start(Result) :-
    read_list(List),
    oc(List,Result).

```

Example run:

```prolog
?- start(Result).
Please enter number, enter "Done" when finished: 1
Please enter number, enter "Done" when finished: 2
Please enter number, enter "Done" when finished: 3
Please enter number, enter "Done" when finished: Done
Result = [[1, 1], [2, 1], [3, 1]] .

```

Also when your code was loaded it gave warnings like:

```prolog
Warning: c:/question.pl:66:
        Singleton variables: [N]

```

Notice in this given code the change of variables, e.g. `N` to `_N`; these are [anonymous variables](https://en.wikipedia.org/wiki/Prolog_syntax_and_semantics#Anonymous_Variables).

* * *

Here is a variation that uses read/1 as you requested.

```prolog
read_list(Items) :-
    write('Please enter number followed by period (.), or enter `0.` when finished: ' ), nl,
    read(Response),
    (
        Response == 0
    ->
        Items = []
    ;
        valid_input(Response,Items)
    ).

valid_input(Response,Items) :-
    (
        number(Response),
        Item = Response
    ->
        % Item was valid
        % so get next number and
        % add to list on backtracking
        read_list(Items0),
        Items = [Item|Items0]
    ;
        % Item was invalid
        % so warn user of invalid number and what they input
        % and get next number.
        % Do not add invalid Item to list on backtracking.
        format('Invalid number: `~w''~n',[Response]),
        read_list(Items0),
        Items = Items0
    ).

```

Example run:

```prolog
?- start(Result).
Please enter number followed by period (.), or enter `0.` when finished: 
|: 1.
Please enter number followed by period (.), or enter `0.` when finished: 
|: 2.
Please enter number followed by period (.), or enter `0.` when finished: 
|: 3.
Please enter number followed by period (.), or enter `0.` when finished: 
|: 0.
Result = [[1, 1], [2, 1], [3, 1]] .

```

The reason I prefer to use [read\_string/5](http://www.swi-prolog.org/pldoc/man?predicate=read_string/5) instead of [read/1](http://www.swi-prolog.org/pldoc/man?predicate=read/1) is having to enter the period for the term does not seem user friendly to me.

---

<div class="post-metadata">

**Author:** ![Danilo](https://avatars.discourse-cdn.com/v4/letter/d/b3f665/32.png) [@Danilo](https://swi-prolog.discourse.group/u/Danilo)\
**Post date:** [April 16, 2019, 11:18am UTC](https://swi-prolog.discourse.group/t/how-to-call-a-predicate-from-another-predicate/541/4 "2019-04-16T11:18:06Z")

</div>

Thank you so much EricGT! 🙂
