【问题标题】:In prolog way to check a list of relations of list elements以序言方式检查列表元素的关系列表
【发布时间】:2014-06-07 15:54:22
【问题描述】:

我是 prolog 的新手,

我无法访问我在此处检查过的列表元素的关系 - Checking a relation of a prolog list element 但我觉得它好像没有完全回答我的问题。

我有拥有汽车的关系,即你需要钱和工作才能拥有汽车

vehicle(car).
own(car, [money, job]).

然后我有一个类似的列表

[[money, job], [car]]

我想检查列表以查看moneyjobcar 元素之前是否存在。

例如,如果我有类似的东西

test([[money, job], [car]]).
OUTPUT: true

如果是的话

test([[job], [car]]).
OUTPUT : false

因为没有钱

所以我的问题是,是否可以检查列表关系的列表元素列表,以查看列表元素是否存在考虑需求,如果不存在,处理此类事情的最佳方法是什么?

编辑:例如,我希望能够测试一堆东西

 vehicle(truck).
 own(truck, [money, job]).

 own(motorbike, [money, job]).
 thing(money).
 own(job, [suit]).
 vehicle(car).
 own(car, [money, job]).

 List of lists : test([[suit], [money, job] , [car, truck, motorbike]]) : True
                 test([[money, job] , [car, truck, motorbike]]) : False since you can't have job without money
                 test([[suit], [job] , [car], [truck], [motorbike]]) :False because you can't have car without owning money, same for truck and motorbike - but will fail on car
                  test([[car], [suit] , [money, job]]): False, because you can't have car without money or job which is after it.

看看我从哪里来?

问候

【问题讨论】:

    标签: list prolog


    【解决方案1】:

    基本上,您想检查某个列表中的每个元素是否都出现在另一个列表中。你可以使用intersection\3来做(还有很多其他的方法):

    test(Want, Have) :-
        own(Want, Need),
        intersection(Need, Have, Need).
    

    测试运行(签名与您提供的不同,但我认为这更有意义,而且您的也有语法错误):

    ?- test(car, [money, job]).
    true.
    
    ?- test(car, [job, money]).
    true.
    
    ?- test(car, [job]).
    false.
    
    ?- test(car, [money, strength]).
    false.
    
    ?- test(car, [money, strength, job]).
    true.
    

    【讨论】:

    • 我进行了编辑,您显示的内容似乎是多余的,我可能只是自己运行(想要,需要)。例如,我可能不会对它进行测试,而是会得到与上面相同的答案,只是去拥有(汽车,[工作])。
    • @Sim 但own(car, [money, strength, job]) 会失败,own(car, [job, money]) 也会失败。我的test 谓词在这种情况下会成功/
    • @SerygeyDymchenko 啊,是的,我明白你的意思了……但这并不是我希望完成的事情
    猜你喜欢
    • 1970-01-01
    • 2015-03-20
    • 2018-11-05
    • 2021-03-18
    • 2016-11-09
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 2015-03-29
    相关资源
    最近更新 更多