2012-11-15 64 views
1

试图找出如何为列表创建“成员”函数。到目前为止,我已经创建了这个,但是我没有接近正确的答案。Prolog成员函数列表


spec([system001,hard_drive(50)]). 
spec([system002,hard_drive(150)]). 

list1(Component):- 
    spec([Component,X|Y]). 

which_system(Component, Component). 

which_system(Component):- 
    list1(Component), 
    which_system(X, Component). 

当我键入which_system(system001). 它的工作原理,但是当我把which_system(hard_drive(50)). 它不会在所有的工作......我不知道如何使它找到hard_drive(50)。

我希望有人可以帮助...

谢谢。

+2

请不要在您的问题得到解答后摧毁您的问题。这个网站的重点是针对较老的问题,以帮助具有类似问题的新人。 –

+0

@ user1726910:改善问题的格式化对他人甚至是礼貌。 – false

回答

3

你这样做复杂得多,需要

which_system(Component, System) :- 
    spec([System|Components]), member(Component, Components). 

这工作也如果你在一个系统中有更多的组件,例如spec([system001, hard_drive(50), hard_drive(100)]).

?- which_system(hard_drive(50), S). 

将实例S指向system001。

+0

哦,对我来说这样做更为复杂。感谢您解决这个问题,现在它确实可行,但是在使用“which_system(hard_drive(50)”)方面还是有可能的方法,而不是使用额外的S: 。谢谢你的方式 – user1726910

+0

true/false。更合适的名称是exists_system(hard_drive(50))。无论如何,Prolog支持具有相同名称和不同参数个数的谓词,只要放下什么不感兴趣的东西即可。'which_system(Component): - spec([ _ |组件]),成员(组件,组件)。“ – CapelliC

+0

谢谢,你一直很有帮助!:) – user1726910