The Coder
The Coder

Reputation: 93

How to compile a prolog question correctly?

I am working on a prolog problem and need some help here.

/*Write a predicate listtran(L,E) which translates a list of Latin number words 
 * to the corresponding list of English number words. */

%% predicate
listtran(L,E).

%% clauses
tran(unus,one).
tran(duo,two).
tran(tres,three).
tran(quattuor,four).
tran(quinque,five).
tran(sex,six).
tran(septem,seven).
tran(octo,eight).
tran(novem,nine).

%% rules
% base case: empty list
listtran([], []).
% inductive cases: 
listtran([L | T0], [E | T1]) :-
  tran(L, E), % translate the head of the list
  listtran(T0, T1). % translate the tail of the list, using recursion

What is needed to be written in predicates and query to test:

?- listtran([unus,novem,duo],X).
should give:
X = [one,nine,two].

and

?- listtran(X,[one,seven,six,two]).
it should return:
X = [unus,septem,sex,duo].

Also, what can be done to avoid the error message:

Clauses of listtran/2 are not together in the source-file

Thanks!

Upvotes: 0

Views: 60

Answers (1)

rajashekar
rajashekar

Reputation: 3783

That is a discontigous predicate error.

Prolog is complaining that all the clauses of a predicate are not defined in one place. You should just delete the listtrans(L, E).(why is it even there?) at the start and the rest should work fine.

An explanation of the error: https://stackoverflow.com/a/40614467/4437190

Upvotes: 1

Related Questions