prolog - prolog:如果两个列表相同但没有特定元素,则谓词为真
问题描述
我想withoutElement(A,Lf,Lg)
在 prolog 中编写一个谓词,如果 list与 list without elementLg
相同,则该谓词为真。Lf
A
例如:
withoutElement(a,[a,b,c,d],[a,b,c,d]).
= 假
和
withoutElement(a,[a,b,c,d],[b,c,d]).
= 真
做这个的最好方式是什么?
解决方案
使用递归定义:
without_element(A, [A|Lg], Lg). % True if element A is
% head of Lf and the tail (Lg)
% is the same.
without_element(A, Lf, [A|Lf]). % If you want to check the other way round
without_element(A, [H|T1], [H|T2]) :- without_element(A, T1, T2). % ↵
% Check first elements are the same, continue checking rest of lists.
推荐阅读
- spring - Spring中ModelAttribute的内部工作
- python - CatBoost——抑制迭代导致网格搜索
- c# - 如何正确锁定集合
- c# - 我想通过电子邮件返回用户详细信息页面,但 id 取值 id = 0 asp.net mvc c#
- javascript - javascript同时执行if else块
- javascript - 如何使用 java 脚本从不同的 html 文件动态加载引导模式内容?
- assembly - x86 汇编中的浮点运算
- java - 我的带有 final 关键字的 java 代码有错误
- c# - Winform TreeView 长时间使用后变白
- python-sphinx - 在 Sphinx 中,如何创建对表特定行的引用?