fdicarlo
fdicarlo

Reputation: 460

Optimising a Prolog script

I'm new to Prolog, we're using in University, and it was asked to define a sainthood, I wrote a quick script that is answering to requirements:

    % List of Saints taken from https://en.wikipedia.org/wiki/List_of_saints

    % Rule to define sainthood
    saint(X) :-
       human(X),
       has_faith(X),
       died(X),
       made_miracles(X).

    % List of humans
    human('Abadiu of phoenix').
    human('Abakuh').
    human('Abamun of Tarnut').
    human('Saint Phoenix').
    human('Fabrizio').
    human('Bob').

   % List of believer
   has_faith('Abadiu of phoenix').
   has_faith('Abakuh').
   has_faith('Abamun of Tarnut').
   has_faith('Saint Phoenix').
   has_faith('Bob').

   % List of died
   died('Abadiu of phoenix').
   died('Abakuh').
   died('Abamun of Tarnut').
   died('Saint Phoenix').

   made_miracles('Abadiu of phoenix').
   made_miracles('Abakuh').
   made_miracles('Abamun of Tarnut').
   made_miracles('Saint Phoenix').

But I would like to improve the code and learning Prolog at the same time, I used:

human(X,['Abadiu of phoenix','Abakuh','Abamun of Tarnut','Saint Phoenix','Fabrizio']).

or

human(['Abadiu of phoenix','Abakuh','Abamun of Tarnut','Saint Phoenix','Fabrizio']).

In order to create an array of humans but without success, where I need to look in order to improve the mentioned code?

Upvotes: 2

Views: 60

Answers (1)

max66
max66

Reputation: 66200

I'm new to Prolog too but I suppose you need findall/3.

Given the following facts

% List of humans
human('Abadiu of phoenix').
human('Abakuh').
human('Abamun of Tarnut').
human('Saint Phoenix').
human('Fabrizio').
human('Bob').

you can define the following clause

humanList(L) :-
  findall(U, human(U), L).

that unify, in L, the list ['Abadiu of phoenix','Abakuh','Abamun of Tarnut','Saint Phoenix','Fabrizio','Bob']

p.s.: what do you call "array", in Prolog is called "list"

Upvotes: 1

Related Questions