Prolog - Search a list for an element, print the list if element is found

list, prolog

Solution

You can define in your program

list_of_my_elements([pear, grape, banana, plum, apples]).

in_my_list_of_elements(X) :- 
    list_of_my_elements(L),
    member(X, L).

Then query

?- in_my_list_of_elements(apple).

I wouldn't re-implement and rename the member predicate.

Problem

So I am trying to write a small program that does the following. I can search for a element inside a list, if the element is found in the list, then the list is printed out to confirm that it has been found. So in basic pseudo - - list of elements - computer, mouse, keyboard, webcam - search for mouse in this list - output the list which mouse has been found in While doing some reading I found something which does what I want to do pretty much. This is below ``` on(Item,[Item|Rest]). on(Item,[DisregardHead|Tail]):- on(Item, Tail). ``` If I type the query - on(apples, [pear, grape, banana, plum, apples]). then it searches through the list, discarding non-relevant elements until it comes to the end and succeeds. What I want to do is write my own list in the editor and work from it by doing the same kind of function to it like above.(Rather than just inputting the list as a query into the console.) Thanks

Original source