2

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.

12
  • 3
    When I load your code into gprolog and do translate([1,2], X). I get X = [one, two]. I'll bet you put a space after translate. Note the translate/0 in the error, meaning "no arguments" for translate. Did you type translate ([1,,2], X).? That won't work. An aside, you can use maplist for this kind of list processing: maplist(totext, Numbers, Names). Try maplist(totext, [1,2,3], Names).
    – lurker
    Feb 22, 2019 at 19:08
  • Is this the full program? Looks like something is missing? Feb 22, 2019 at 19:09
  • @WillemVanOnsem nothing is missing. It works as-is.
    – lurker
    Feb 22, 2019 at 19:09
  • This is the full program, I have it saved as "test.pl". I open gprolog then do consult('test.pl'). and try it and it doesn't work.
    – user5597655
    Feb 22, 2019 at 19:09
  • @lurker: what I mean is that, since we do not get the error, the two look not in sync: the program that produces the error, and the one shared here. Feb 22, 2019 at 19:10

1 Answer 1

2

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?

3
  • 1
    Yep I'm running Ubuntu. I got gprolog by doing sudo apt install gprolog. I will try to get it some other way
    – user5597655
    Feb 22, 2019 at 19:24
  • 1
    Same here. I'm on Ubuntu and had done a fresh build from source.
    – lurker
    Feb 22, 2019 at 19:34
  • 1
    Rebuilding gprolog 1.4.5 from source on Ubuntu fixed this issue and the code works. Thanks!
    – user5597655
    Feb 22, 2019 at 19:40

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.