1
votes

Trying to figure out how to create a "member" function for a list. So far I've created this but I'm getting no where near to the correct answer.


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).

When I type in which_system(system001). it works but when i put in which_system(hard_drive(50)). it wont work at all... I dont know how to make it to find hard_drive(50).

I hope someone can help...

Thanks.

1
Please don't destroy your question once it has been answered. The whole point of this site is for older questions to help newer people with similar issues.Niet the Dark Absol
@user1726910: It would even be a courtesy to others to improve the formatting of your question.false

1 Answers

3
votes

you're doing it more complex than needed

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

This works also if you'll have more components in a system, for instance spec([system001, hard_drive(50), hard_drive(100)])..

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

will instance S to system001.