本文介绍了Prolog-回文函的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在Prolog中编写谓词palindrome/1
,当且仅当其列表输入包含回文列表时,才为真.
I am trying to write a predicate palindrome/1
in Prolog that is true if and only if its list input consists of a palindromic list.
例如:
?- palindrome([1,2,3,4,5,4,3,2,1]).
是真的.
有什么想法或解决方案吗?
Any ideas or solutions?
推荐答案
回文列表是一个向后读取相同列表的列表,因此您可以反转该列表以检查它是否产生相同的列表:
A palindrome list is a list which reads the same backwards, so you can reverse the list to check whether it yields the same list:
palindrome(L):-
reverse(L, L).
这篇关于Prolog-回文函的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!