1
votes

I'm trying to write a prolog program to demonstrate how a cut can be used to increase efficiency. I'm using the following member declaration to search through a list.

member(Element, [Element | _]).
member(Element, [_ | List]) :- member(Element, List).

The function seems to be working just fine ex:

 ?- member(1,[3,1,4]).
true 

The issue that i'm running into is I need to be able to use member with a predeclared list. like so:

someList([3,1,4]).

 ?- somelist(X).
X = [3, 1, 4].

3 ?- member(1,somelist).
false.

Even though somelist is defined as [3,1,4] which works in the first test, when called this returns false. I just need to know what I'm doing wrong that ?- member(1,somelist). is returning false. Any help would be greatly appreciated.

1
someList is a predicate, not a function. someList(X) as you see does a query and determines that the query is satisfied by instantiating X with [3,1,4]. It's result is, therefore "true" (it succeeded), but it's not a function return value. Nor is the result X = [3,1,4] a return value of someList as a "function". Prolog doesn't use functions. Try: someList(L), member(1, L). which is to say, L is someList AND 1 is a member of L. - lurker

1 Answers

0
votes

someList is the name of a proposition that does not exist, not a list, so it'll always produce false/no. Prolog accepts queries and produces answers. What you seem to be asking for is the concept of variables in an imperative language, which doesn't exist here. You can, for example, ask Prolog for all of the lists that have 1 as their member with member(1, X), or for a particular list member(1, [3,1,4]), but you can't store a list and ask about it in this way.

Prolog works with a Knowledge Base, and infers truthness through rules/predicates, facts and propositions that it knows. You need to ask it what you want to know.