Prolog 比较相等值并打印相等值的步骤
我想比较两个值X和C1,正如你在代码中看到的,X是C的反方向值,如果两个值相等,那么它应该打印比较后的值。请告诉我怎么做。这是打印回文数字,如。。。1,11,22,33,44,55,66,77,88,99101Prolog 比较相等值并打印相等值的步骤,prolog,Prolog,我想比较两个值X和C1,正如你在代码中看到的,X是C的反方向值,如果两个值相等,那么它应该打印比较后的值。请告诉我怎么做。这是打印回文数字,如。。。1,11,22,33,44,55,66,77,88,99101 go(N):- write(0),nl, go(0,N). go(_,0):- !. go(A,C):- A1 is A, C1 is A1 + 1, /* write(C1),nl,*
go(N):-
write(0),nl,
go(0,N).
go(_,0):- !.
go(A,C):-
A1 is A,
C1 is A1 + 1,
/* write(C1),nl,*/
rev(C1,X),
/* write(X),nl,*/
/* To compare the Value of X and C1 and print if compared value is true*/
NewC is C-1,
go(C1,NewC).
rev(Q,E):-
name(Q, Q1),
reverse(Q1,E1),
name(E,E1).
对于CLP(FD)和DCGs来说,描述回文数实际上是一项很好的任务。首先,让我们描述回文数字的数字是什么样子的:
:- use_module(library(clpfd)).
palindromedigits(Digits) :- % Digits are palindrome digits if
Digits ins 0..9, % they are between 0 and 9
Digits = [H|_], % and the first digit...
H #\= 0, % ... is different from 0
phrase(palindrome, Digits). % and they form a palindrome
palindrome --> % a palindrome is
[]. % an empty list
palindrome --> % or
[_]. % a list with a single element
palindrome --> % or
[A], % an element A
palindrome, % followed by a palindrome
[A]. % followed by an element A
要测试某个数字是否为回文,可以将其转换为数字列表,并且回文数字/1
必须保留该列表。要生成这样的数字,您可以使用length/2
来描述所有可能长度的列表,palindromedigits/1
必须再次对这些列表保持不变,并且数字必须乘以它们各自的十次幂并求和。由于前导零被回文数字/1
排除,如果要将其包含在回文数字中,则必须为0
添加一个事实。可能看起来像这样:
palindromenumber(0). % 0 is a palindromenumber
palindromenumber(PN) :- % rule for testing numbers
number(PN), % succeeds if PN is a number
number_codes(PN,C), % C is a list of codes corresponding to the digits
maplist(plus(48),Digits,C), % codes and digits are off by 48
palindromedigits(Digits). % Digits is a palindrome
palindromenumber(PN) :- % rule for generating numbers
var(PN), % succeeds if PN is a variable
length(Digits,_), % Digits is a list of length 0,1,2,...
palindromedigits(Digits), % Digits is a palindrome
digits_number_(Digits,PN,1,0), % Digits correspond to the number PN
label(Digits). % labels the list Digits with actual numbers
请注意,数字对应的代码关闭48,因此使用maplist/3
实现目标,例如:
?- number_codes(123,C), maplist(plus(48),Digits,C).
C = [49, 50, 51], % <- the codes
Digits = [1, 2, 3]. % <- the actual digits
请注意,数字按相反顺序乘以幂并不重要,因为它是一个回文数
现在,您可以查询回文数字:
?- palindromenumber(PN).
PN = 0 ;
PN = 1 ;
PN = 2 ;
.
.
.
PN = 33 ;
PN = 44 ;
PN = 55 ;
.
.
.
PN = 666 ;
PN = 676 ;
PN = 686 ;
.
.
.
PN = 7667 ;
PN = 7777 ;
PN = 7887
.
.
.
或者,您可以测试数字是否为回文:
?- palindromenumber(121).
true ;
false.
?- palindromenumber(123).
false.
?- palindromenumber(12321).
true ;
false.
编辑
为了解决你评论中的问题,你可以通过描述这样一个序列和它的长度之间的关系来解决。所以你会有一个算术为2的谓词,而不是算术为1的谓词。让我们给它一个很好的描述性名称,比如说firstN\u回文数/2
。实际实现由一个谓词描述,该谓词带有一个附加参数,用于保存要检查的当前候选对象。由于您希望以1开始序列,因此这将是第一个参数n_palindromicnumbers_/3将使用以下参数调用:
firstN_palindromicnumbers(N,PNs) :-
firstN_palindromicnumbers_(N,PNs,1). % sequence starts with 1
通过递归,保存候选项的参数将增加1,而第一个参数N
,将在每次候选项变成实际回文数时减少。因此谓词最终以N
为0
,一个空列表和一个我们不关心的候选项结束。这将是基本情况。否则,列表的开头是序列(剩余部分)中最小的回文数。您可以重复使用上面的目标number\u code/2
和maplist/3
来描述与当前候选对象相对应的数字列表,并使用DCG回文//0
来声明数字必须是回文。谓词回文数字/1
中的其他目标不需要,因为候选数字将是1,2,3,…,因此由0到9之间的(至少一个)数字组成,不带前导零。您可以在序言中这样表达:
firstN_palindromicnumbers_(0,[],_C). % base case
firstN_palindromicnumbers_(N1,[C0|PNs],C0) :- % case: C0 is a palindrome
N1 #> 0, % sequence is not of desired length yet
number_codes(C0,Codes),
maplist(plus(48),Digits,Codes),
phrase(palindrome, Digits), % digits form a palindrome
N0 #= N1-1, % sequence of length N1-1 has to be described yet
C1 #= C0+1, % C1 is the next candidate
firstN_palindromicnumbers_(N0,PNs,C1). % PNs is the rest of the sequence
firstN_palindromicnumbers_(N1,PNs,C0) :- % case: C0 ain't a palindrome
N1 #> 0, % sequence is not of desired length yet
number_codes(C0,Codes),
maplist(plus(48),Digits,Codes),
\+ phrase(palindrome, Digits), % digits don't form a palindrome
C1 #= C0+1, % C1 is the next candidate
firstN_palindromicnumbers_(N1,PNs,C1). % PNs is the rest of the sequence
现在,您可以查询谓词以获得给定长度的回文数序列(注意,使用SWI Prolog,您可能需要点击w才能查看整个列表):
最一般的查询也会得到预期的答案:
?- firstN_palindromicnumbers(N,PNs).
N = 0,
PNs = [] ;
N = 1,
PNs = [1] ;
N = 2,
PNs = [1, 2] ;
N = 3,
PNs = [1, 2, 3] ;
.
.
.
您可以执行类似于
C1==X->writeln(X)
的操作。当我这样做时,out:0-9为False,我的输入为go(15)。A1是一个的用途是什么?go(N)
到底应该做什么?如何C1==X->writeln(X);是的。
@DanielLyons仍然没有处理我的代码。我们如何才能做到,如果我把回文数(100)放进去,它会从1@ZainAli当前位置我更新了我的答案以解决那个问题。
?- firstN_palindromicnumbers(15,PNs).
PNs = [1, 2, 3, 4, 5, 6, 7, 8, 9|...] [write] % <- hit the w key
PNs = [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66] ;
false.
?- firstN_palindromicnumbers(25,PNs).
PNs = [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99, 101, 111, 121, 131, 141, 151, 161] ;
false.
?- firstN_palindromicnumbers(N,[1,2,3,4,5]).
N = 5 ;
false.
?- firstN_palindromicnumbers(N,[0|_]).
false.
?- firstN_palindromicnumbers(N,[1,2,3,4,11]).
false.
?- firstN_palindromicnumbers(N,PNs).
N = 0,
PNs = [] ;
N = 1,
PNs = [1] ;
N = 2,
PNs = [1, 2] ;
N = 3,
PNs = [1, 2, 3] ;
.
.
.