0

I am trying to create a Palindrome Programme on Visual Prolog that checks user input number. I somehow wrote some codes BUT it shows error and it is difficult for me to remove errors. Please, I need help on this code.

DOMAINS Num,Temp,Reverse=integer

PREDICATES palindrome

CLAUSES *

palindrome:-
   Reverse=:=0,
   write("Enter a number to check ."),
   readint(Number),
   Temp=Number

loop(Temp=/=0) :-
   Reverse=Reverse*10,
   Reverse=Reverse+ Temp mod 10,
   Temp=Temp/10, 
   false.
(Number=:=Reverse->
   write("The Number ",Number," is a Palindrome "),
   fail ; Number=/=Reverse->
   write("The Number ",Number," is not a Palindrome.") ; .

GOAL palindrome.

2
  • Reverse=Reverse*10 is False condition. Use IS operator if you are going to set some numerical value to a variable. Aug 12, 2013 at 14:33
  • Visual Prolog should also accept more standard Prolog. Do you agree on some answer more 'Prolog like' ?
    – CapelliC
    Aug 12, 2013 at 17:49

1 Answer 1

3

In writing a prolog program, it can be helpful to write down a clear, specific problem statement and decompose the problem. Something like:

A number is palindromic, IF it is an integer, and IF its digits are identical when reversed, ignoring its sign.

That leads us to this interface predicate which pretty much recapitulates the problem statement, thus:

%---------------------
% the public interface
%---------------------
palindromic_number(X) :- % A number is palindromic if
  integer(X) ,           % - it is an integer, and
  X >= 0 ,               % - it is greater than or equal to zero, and
  reverse_digits(X,0,X)  % - its decimal value is the same if you reverse its decimal digits
  .                      % ... OR ...
palindromic_number(X) :- % it is palindromic, if
  integer(X) ,           % - it is an integer, and
  X < 0 ,                % - it is less than zero, and
  X1 is - X ,            % - its absolute value
  palindromic_number(X)  % - is palindromic
  .                      % Easy!

Now, all we have to do is figure out how to reverse the digits of a number. Given that we've eliminated dealing with the sign, above, it's easy: Strip digits from the right end, adding them to the left end of the result, until we hit zero.

A useful idiom in prolog is to have a public predicate that fronts a private workder predicate that often takes an accumulator wherein the final result is built up as you recursively work the problem. Also, in this case (and many others), there is usually a general case and one or a few special cases. Here, our special case, which terminates the compuation is when the source value is zero.

Which leads us to this definition of "how to reverse the digits of a number":

% ---------------------
% The worker predicate
% ---------------------
reverse_digits(0,T,T).    % once we hit zero, the accumulator has the reversed number. Unify the accumulator with the desired result.
reverse_digits(X,T,Y) :-  % Otherwise...
  X > 0 ,                 % - if X > 0,
  X1 is X /   10 ,        % - compute the next X
  D  is X mod 10 ,        % - compute the nexst digit
  T1 is 10*T + D ,        % - scale the accumulator and add the digit
reverse_digits(X1,T1,Y)   % - recurse down.
.                         % - easy!

Another approach, of course, would be to convert the number into a string (which is a list of individual characters), reverse that list using the built-in reverse/2 predicate and unify that with the original value. I doubt that is what your instructor is looking for, however.

Not the answer you're looking for? Browse other questions tagged or ask your own question.