Prolog: own Implementation of member fails / general Prolog problem
up vote
-1
down vote
favorite
So I am new to Prolog and have implemented my own implementation of member.
#contains element
mymember(A, [A|T]).
mymember(A, [X|T]) :- mymember(A, T).
After looking up this should be the classical implementation, but when I test it, it fails:
[trace] ?- mymember(2, [1, 2, 3]).
Call: (8) mymember(2, [1, 2, 3]) ?
Call: (9) mymember(2, [2, 3]) ?
Call: (10) mymember(2, [3]) ?
Call: (11) mymember(2, ) ?
Fail: (11) mymember(2, ) ?
Fail: (10) mymember(2, [3]) ?
Fail: (9) mymember(2, [2, 3]) ?
Fail: (8) mymember(2, [1, 2, 3]) ?
false.
Now what confuses me is that Prolog only uses the second case until it fails but never tries to match my first case. Does anybody know the reason of this behavior?
(I'm using SWI-Prolog Version 7.6.4)
EDIT: Well now I know what my problem was. Since I don't have syntax highlighting I didn't notice that I actually didn't use the correct character for one line comments. After replacing '#' with '%' my first case now matches in the second call. A typical beginner mistake I guess...
prolog
New contributor
add a comment |
up vote
-1
down vote
favorite
So I am new to Prolog and have implemented my own implementation of member.
#contains element
mymember(A, [A|T]).
mymember(A, [X|T]) :- mymember(A, T).
After looking up this should be the classical implementation, but when I test it, it fails:
[trace] ?- mymember(2, [1, 2, 3]).
Call: (8) mymember(2, [1, 2, 3]) ?
Call: (9) mymember(2, [2, 3]) ?
Call: (10) mymember(2, [3]) ?
Call: (11) mymember(2, ) ?
Fail: (11) mymember(2, ) ?
Fail: (10) mymember(2, [3]) ?
Fail: (9) mymember(2, [2, 3]) ?
Fail: (8) mymember(2, [1, 2, 3]) ?
false.
Now what confuses me is that Prolog only uses the second case until it fails but never tries to match my first case. Does anybody know the reason of this behavior?
(I'm using SWI-Prolog Version 7.6.4)
EDIT: Well now I know what my problem was. Since I don't have syntax highlighting I didn't notice that I actually didn't use the correct character for one line comments. After replacing '#' with '%' my first case now matches in the second call. A typical beginner mistake I guess...
prolog
New contributor
This works for me locally. Did you reload your file, or is there something else in your file that results in the file being wrongly interpreted?
– Willem Van Onsem
2 days ago
Based upon the code you're showing, the second step going to the third step in the trace doesn't make sense. So double check your code. Are you sure what you are showing here is what you entered and tested?
– lurker
2 days ago
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
So I am new to Prolog and have implemented my own implementation of member.
#contains element
mymember(A, [A|T]).
mymember(A, [X|T]) :- mymember(A, T).
After looking up this should be the classical implementation, but when I test it, it fails:
[trace] ?- mymember(2, [1, 2, 3]).
Call: (8) mymember(2, [1, 2, 3]) ?
Call: (9) mymember(2, [2, 3]) ?
Call: (10) mymember(2, [3]) ?
Call: (11) mymember(2, ) ?
Fail: (11) mymember(2, ) ?
Fail: (10) mymember(2, [3]) ?
Fail: (9) mymember(2, [2, 3]) ?
Fail: (8) mymember(2, [1, 2, 3]) ?
false.
Now what confuses me is that Prolog only uses the second case until it fails but never tries to match my first case. Does anybody know the reason of this behavior?
(I'm using SWI-Prolog Version 7.6.4)
EDIT: Well now I know what my problem was. Since I don't have syntax highlighting I didn't notice that I actually didn't use the correct character for one line comments. After replacing '#' with '%' my first case now matches in the second call. A typical beginner mistake I guess...
prolog
New contributor
So I am new to Prolog and have implemented my own implementation of member.
#contains element
mymember(A, [A|T]).
mymember(A, [X|T]) :- mymember(A, T).
After looking up this should be the classical implementation, but when I test it, it fails:
[trace] ?- mymember(2, [1, 2, 3]).
Call: (8) mymember(2, [1, 2, 3]) ?
Call: (9) mymember(2, [2, 3]) ?
Call: (10) mymember(2, [3]) ?
Call: (11) mymember(2, ) ?
Fail: (11) mymember(2, ) ?
Fail: (10) mymember(2, [3]) ?
Fail: (9) mymember(2, [2, 3]) ?
Fail: (8) mymember(2, [1, 2, 3]) ?
false.
Now what confuses me is that Prolog only uses the second case until it fails but never tries to match my first case. Does anybody know the reason of this behavior?
(I'm using SWI-Prolog Version 7.6.4)
EDIT: Well now I know what my problem was. Since I don't have syntax highlighting I didn't notice that I actually didn't use the correct character for one line comments. After replacing '#' with '%' my first case now matches in the second call. A typical beginner mistake I guess...
prolog
prolog
New contributor
New contributor
edited yesterday
New contributor
asked 2 days ago
slukas
11
11
New contributor
New contributor
This works for me locally. Did you reload your file, or is there something else in your file that results in the file being wrongly interpreted?
– Willem Van Onsem
2 days ago
Based upon the code you're showing, the second step going to the third step in the trace doesn't make sense. So double check your code. Are you sure what you are showing here is what you entered and tested?
– lurker
2 days ago
add a comment |
This works for me locally. Did you reload your file, or is there something else in your file that results in the file being wrongly interpreted?
– Willem Van Onsem
2 days ago
Based upon the code you're showing, the second step going to the third step in the trace doesn't make sense. So double check your code. Are you sure what you are showing here is what you entered and tested?
– lurker
2 days ago
This works for me locally. Did you reload your file, or is there something else in your file that results in the file being wrongly interpreted?
– Willem Van Onsem
2 days ago
This works for me locally. Did you reload your file, or is there something else in your file that results in the file being wrongly interpreted?
– Willem Van Onsem
2 days ago
Based upon the code you're showing, the second step going to the third step in the trace doesn't make sense. So double check your code. Are you sure what you are showing here is what you entered and tested?
– lurker
2 days ago
Based upon the code you're showing, the second step going to the third step in the trace doesn't make sense. So double check your code. Are you sure what you are showing here is what you entered and tested?
– lurker
2 days ago
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
Your implementations works for me.
mymember(2,[1,2,3]).
Call: (8) mymember(2, [1, 2, 3]) ? creep
Call: (9) mymember(2, [2, 3]) ? creep
Exit: (9) mymember(2, [2, 3]) ? creep
Exit: (8) mymember(2, [1, 2, 3]) ? creep
true .
Though you could lose some of the variables as they are singletons, like this:
mymember(A, [A|_]).
mymember(A, [_|T]) :- mymember(A, T).
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Your implementations works for me.
mymember(2,[1,2,3]).
Call: (8) mymember(2, [1, 2, 3]) ? creep
Call: (9) mymember(2, [2, 3]) ? creep
Exit: (9) mymember(2, [2, 3]) ? creep
Exit: (8) mymember(2, [1, 2, 3]) ? creep
true .
Though you could lose some of the variables as they are singletons, like this:
mymember(A, [A|_]).
mymember(A, [_|T]) :- mymember(A, T).
add a comment |
up vote
0
down vote
Your implementations works for me.
mymember(2,[1,2,3]).
Call: (8) mymember(2, [1, 2, 3]) ? creep
Call: (9) mymember(2, [2, 3]) ? creep
Exit: (9) mymember(2, [2, 3]) ? creep
Exit: (8) mymember(2, [1, 2, 3]) ? creep
true .
Though you could lose some of the variables as they are singletons, like this:
mymember(A, [A|_]).
mymember(A, [_|T]) :- mymember(A, T).
add a comment |
up vote
0
down vote
up vote
0
down vote
Your implementations works for me.
mymember(2,[1,2,3]).
Call: (8) mymember(2, [1, 2, 3]) ? creep
Call: (9) mymember(2, [2, 3]) ? creep
Exit: (9) mymember(2, [2, 3]) ? creep
Exit: (8) mymember(2, [1, 2, 3]) ? creep
true .
Though you could lose some of the variables as they are singletons, like this:
mymember(A, [A|_]).
mymember(A, [_|T]) :- mymember(A, T).
Your implementations works for me.
mymember(2,[1,2,3]).
Call: (8) mymember(2, [1, 2, 3]) ? creep
Call: (9) mymember(2, [2, 3]) ? creep
Exit: (9) mymember(2, [2, 3]) ? creep
Exit: (8) mymember(2, [1, 2, 3]) ? creep
true .
Though you could lose some of the variables as they are singletons, like this:
mymember(A, [A|_]).
mymember(A, [_|T]) :- mymember(A, T).
answered 2 days ago
Richard Ardelean
912
912
add a comment |
add a comment |
slukas is a new contributor. Be nice, and check out our Code of Conduct.
slukas is a new contributor. Be nice, and check out our Code of Conduct.
slukas is a new contributor. Be nice, and check out our Code of Conduct.
slukas is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238586%2fprolog-own-implementation-of-member-fails-general-prolog-problem%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
This works for me locally. Did you reload your file, or is there something else in your file that results in the file being wrongly interpreted?
– Willem Van Onsem
2 days ago
Based upon the code you're showing, the second step going to the third step in the trace doesn't make sense. So double check your code. Are you sure what you are showing here is what you entered and tested?
– lurker
2 days ago