user5597655
user5597655

Reputation:

How to convert a list of numbers to a list of words in Prolog?

I am trying to write a Prolog program such that given a list of numbers like [1, 2, 3], it will convert them into a list of words representing those numbers ['one', 'two', 'three'].

My code so far:

totext(0, 'zero').
totext(1, 'one').
totext(2, 'two').
totext(3, 'three').
totext(4, 'four').
totext(5, 'five').
totext(6, 'six').
totext(7, 'seven').
totext(8, 'eight').
totext(9, 'nine').

translate([], []).
translate([Head|Rest], [TranslatedHead|TranslatedRest]) :-
   totext(Head, TranslatedHead),
   translate(Rest, TranslatedRest).

When I load up gprolog and consult the file, if I do:

translate([], X).

I correctly get:

X = []
yes

But when I do

translate([1,2], X).

I get:

uncaught exception: error(existence_error(procedure,totext/0),translate/0)

I am very new to Prolog (this is my first Prolog program), and I have no idea what is going wrong here. Any ideas? Thanks.

Upvotes: 2

Views: 1003

Answers (1)

Paulo Moura
Paulo Moura

Reputation: 18663

Your code is corrected but your build of GNU Prolog is broken. Recompiling GNU Prolog from sources should fix it. The common symptom of a broken GNU Prolog build is predicate existence errors where the predicates, always reported as having arity zero, don't usually exist in the code being called.

P.S. The broken GNU Prolog installations seem to occur usually when using Ubuntu. Can you confirm that's also your case?

Upvotes: 2

Related Questions