First elements of list of list Prolog
I m studying Prolog and i see this code
foo(,).
foo([[A,_ ]|L], [A|P]) :-foo(L ,P).
The result say that this code take N element of list of list,
Ad example if we give this query:
?foo([[car],[house],[man]],X)
X= [c,h,m]
At first read i see that something wrong.
For me this code take the tail of list of list and the rest of first element of the list , so for me first expansion will be (trace)
foo([[house],[man]], ar)
foo([[man]], ouse)
foo(, an)
false.
I try to compile with swi-prolog and give this trace:
[trace] ?- trace,foo([[car],[house],[man]],X).
Call: (9) foo([[car], [house], [man]], _1016) ? creep
Fail: (9) foo([[car], [house], [man]], _1016) ? creep
false.
What are I wrong?
prolog
add a comment |
I m studying Prolog and i see this code
foo(,).
foo([[A,_ ]|L], [A|P]) :-foo(L ,P).
The result say that this code take N element of list of list,
Ad example if we give this query:
?foo([[car],[house],[man]],X)
X= [c,h,m]
At first read i see that something wrong.
For me this code take the tail of list of list and the rest of first element of the list , so for me first expansion will be (trace)
foo([[house],[man]], ar)
foo([[man]], ouse)
foo(, an)
false.
I try to compile with swi-prolog and give this trace:
[trace] ?- trace,foo([[car],[house],[man]],X).
Call: (9) foo([[car], [house], [man]], _1016) ? creep
Fail: (9) foo([[car], [house], [man]], _1016) ? creep
false.
What are I wrong?
prolog
add a comment |
I m studying Prolog and i see this code
foo(,).
foo([[A,_ ]|L], [A|P]) :-foo(L ,P).
The result say that this code take N element of list of list,
Ad example if we give this query:
?foo([[car],[house],[man]],X)
X= [c,h,m]
At first read i see that something wrong.
For me this code take the tail of list of list and the rest of first element of the list , so for me first expansion will be (trace)
foo([[house],[man]], ar)
foo([[man]], ouse)
foo(, an)
false.
I try to compile with swi-prolog and give this trace:
[trace] ?- trace,foo([[car],[house],[man]],X).
Call: (9) foo([[car], [house], [man]], _1016) ? creep
Fail: (9) foo([[car], [house], [man]], _1016) ? creep
false.
What are I wrong?
prolog
I m studying Prolog and i see this code
foo(,).
foo([[A,_ ]|L], [A|P]) :-foo(L ,P).
The result say that this code take N element of list of list,
Ad example if we give this query:
?foo([[car],[house],[man]],X)
X= [c,h,m]
At first read i see that something wrong.
For me this code take the tail of list of list and the rest of first element of the list , so for me first expansion will be (trace)
foo([[house],[man]], ar)
foo([[man]], ouse)
foo(, an)
false.
I try to compile with swi-prolog and give this trace:
[trace] ?- trace,foo([[car],[house],[man]],X).
Call: (9) foo([[car], [house], [man]], _1016) ? creep
Fail: (9) foo([[car], [house], [man]], _1016) ? creep
false.
What are I wrong?
prolog
prolog
edited Jan 3 at 2:50
false
10.2k773150
10.2k773150
asked Jan 2 at 18:10
theantomctheantomc
1209
1209
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Obtaining the first elements
The pattern [A, _]
in your clause is wrong, or at at least not generic enough. [A, _]
unifies with a list that contains exactly two elements, but this will thus fail for lists with more than two elements, or with one elements, like you found out.
You need to use the [A|_]
pattern: indeed a list where the head is A
, and we are not interested in the rest (tail). like:
foo(,).
foo([[A|_]|L], [A|P]) :- foo(L, P).
That being said, you can simplify this, by implementing a predicate that takes the head of a list:
head([H|_], H).
and then make use of maplist/3
[swi-doc]:
foo(A, B) :-
maplist(head, A, B).
maplist
will thus call head
like head(Ai, Bi)
, with Ai
and Bi
elements of A
and B
respectively.
Obtaining a substring with the first character
but based on the sample output, this is not what you want: you also want to obtain the first "character" of the atom, we can do that by using string_chars/2
[swi-doc]:
head_first([A|_], C) :-
string_chars(A, [C|_]).
and then define foo/2
again with maplist/3
[swi-doc]:
foo(A, B) :-
maplist(head_first, A, B).
we then obtain:
?- foo([[car],[house],[man]], X).
X = [c, h, m].
maybe this my question will do stupid.. [A|P] in the foo/2 don't take a first list (in list of list) and give the tail P from foot(L,P). Because i understand well [[A|_]|L], but [A|P] i have some doubts. By the way, always clear. Sorry for my more question on prolog, i m studying the subject.
– theantomc
Jan 2 at 20:19
With[A|P]
we are at that moment constructing (well given we read in in that direction) a list, soA
is the first element of the result, andP
the tail (remaining list). You actually wrote the[A|P]
part yourself in your question.
– Willem Van Onsem
Jan 2 at 20:21
"constructing " you mean a a variable part? (X = [c, h, m]). Because for my first look on the code I thought that this part is refers to list of the list (first element in the query), but i m wrong sure( because is second element)
– theantomc
Jan 2 at 20:28
@theantomc: yes, here we are constructing theX = [c, h, m]
part.
– Willem Van Onsem
Jan 2 at 20:29
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54011157%2ffirst-elements-of-list-of-list-prolog%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Obtaining the first elements
The pattern [A, _]
in your clause is wrong, or at at least not generic enough. [A, _]
unifies with a list that contains exactly two elements, but this will thus fail for lists with more than two elements, or with one elements, like you found out.
You need to use the [A|_]
pattern: indeed a list where the head is A
, and we are not interested in the rest (tail). like:
foo(,).
foo([[A|_]|L], [A|P]) :- foo(L, P).
That being said, you can simplify this, by implementing a predicate that takes the head of a list:
head([H|_], H).
and then make use of maplist/3
[swi-doc]:
foo(A, B) :-
maplist(head, A, B).
maplist
will thus call head
like head(Ai, Bi)
, with Ai
and Bi
elements of A
and B
respectively.
Obtaining a substring with the first character
but based on the sample output, this is not what you want: you also want to obtain the first "character" of the atom, we can do that by using string_chars/2
[swi-doc]:
head_first([A|_], C) :-
string_chars(A, [C|_]).
and then define foo/2
again with maplist/3
[swi-doc]:
foo(A, B) :-
maplist(head_first, A, B).
we then obtain:
?- foo([[car],[house],[man]], X).
X = [c, h, m].
maybe this my question will do stupid.. [A|P] in the foo/2 don't take a first list (in list of list) and give the tail P from foot(L,P). Because i understand well [[A|_]|L], but [A|P] i have some doubts. By the way, always clear. Sorry for my more question on prolog, i m studying the subject.
– theantomc
Jan 2 at 20:19
With[A|P]
we are at that moment constructing (well given we read in in that direction) a list, soA
is the first element of the result, andP
the tail (remaining list). You actually wrote the[A|P]
part yourself in your question.
– Willem Van Onsem
Jan 2 at 20:21
"constructing " you mean a a variable part? (X = [c, h, m]). Because for my first look on the code I thought that this part is refers to list of the list (first element in the query), but i m wrong sure( because is second element)
– theantomc
Jan 2 at 20:28
@theantomc: yes, here we are constructing theX = [c, h, m]
part.
– Willem Van Onsem
Jan 2 at 20:29
add a comment |
Obtaining the first elements
The pattern [A, _]
in your clause is wrong, or at at least not generic enough. [A, _]
unifies with a list that contains exactly two elements, but this will thus fail for lists with more than two elements, or with one elements, like you found out.
You need to use the [A|_]
pattern: indeed a list where the head is A
, and we are not interested in the rest (tail). like:
foo(,).
foo([[A|_]|L], [A|P]) :- foo(L, P).
That being said, you can simplify this, by implementing a predicate that takes the head of a list:
head([H|_], H).
and then make use of maplist/3
[swi-doc]:
foo(A, B) :-
maplist(head, A, B).
maplist
will thus call head
like head(Ai, Bi)
, with Ai
and Bi
elements of A
and B
respectively.
Obtaining a substring with the first character
but based on the sample output, this is not what you want: you also want to obtain the first "character" of the atom, we can do that by using string_chars/2
[swi-doc]:
head_first([A|_], C) :-
string_chars(A, [C|_]).
and then define foo/2
again with maplist/3
[swi-doc]:
foo(A, B) :-
maplist(head_first, A, B).
we then obtain:
?- foo([[car],[house],[man]], X).
X = [c, h, m].
maybe this my question will do stupid.. [A|P] in the foo/2 don't take a first list (in list of list) and give the tail P from foot(L,P). Because i understand well [[A|_]|L], but [A|P] i have some doubts. By the way, always clear. Sorry for my more question on prolog, i m studying the subject.
– theantomc
Jan 2 at 20:19
With[A|P]
we are at that moment constructing (well given we read in in that direction) a list, soA
is the first element of the result, andP
the tail (remaining list). You actually wrote the[A|P]
part yourself in your question.
– Willem Van Onsem
Jan 2 at 20:21
"constructing " you mean a a variable part? (X = [c, h, m]). Because for my first look on the code I thought that this part is refers to list of the list (first element in the query), but i m wrong sure( because is second element)
– theantomc
Jan 2 at 20:28
@theantomc: yes, here we are constructing theX = [c, h, m]
part.
– Willem Van Onsem
Jan 2 at 20:29
add a comment |
Obtaining the first elements
The pattern [A, _]
in your clause is wrong, or at at least not generic enough. [A, _]
unifies with a list that contains exactly two elements, but this will thus fail for lists with more than two elements, or with one elements, like you found out.
You need to use the [A|_]
pattern: indeed a list where the head is A
, and we are not interested in the rest (tail). like:
foo(,).
foo([[A|_]|L], [A|P]) :- foo(L, P).
That being said, you can simplify this, by implementing a predicate that takes the head of a list:
head([H|_], H).
and then make use of maplist/3
[swi-doc]:
foo(A, B) :-
maplist(head, A, B).
maplist
will thus call head
like head(Ai, Bi)
, with Ai
and Bi
elements of A
and B
respectively.
Obtaining a substring with the first character
but based on the sample output, this is not what you want: you also want to obtain the first "character" of the atom, we can do that by using string_chars/2
[swi-doc]:
head_first([A|_], C) :-
string_chars(A, [C|_]).
and then define foo/2
again with maplist/3
[swi-doc]:
foo(A, B) :-
maplist(head_first, A, B).
we then obtain:
?- foo([[car],[house],[man]], X).
X = [c, h, m].
Obtaining the first elements
The pattern [A, _]
in your clause is wrong, or at at least not generic enough. [A, _]
unifies with a list that contains exactly two elements, but this will thus fail for lists with more than two elements, or with one elements, like you found out.
You need to use the [A|_]
pattern: indeed a list where the head is A
, and we are not interested in the rest (tail). like:
foo(,).
foo([[A|_]|L], [A|P]) :- foo(L, P).
That being said, you can simplify this, by implementing a predicate that takes the head of a list:
head([H|_], H).
and then make use of maplist/3
[swi-doc]:
foo(A, B) :-
maplist(head, A, B).
maplist
will thus call head
like head(Ai, Bi)
, with Ai
and Bi
elements of A
and B
respectively.
Obtaining a substring with the first character
but based on the sample output, this is not what you want: you also want to obtain the first "character" of the atom, we can do that by using string_chars/2
[swi-doc]:
head_first([A|_], C) :-
string_chars(A, [C|_]).
and then define foo/2
again with maplist/3
[swi-doc]:
foo(A, B) :-
maplist(head_first, A, B).
we then obtain:
?- foo([[car],[house],[man]], X).
X = [c, h, m].
edited Jan 2 at 18:51
answered Jan 2 at 18:31
Willem Van OnsemWillem Van Onsem
150k16145235
150k16145235
maybe this my question will do stupid.. [A|P] in the foo/2 don't take a first list (in list of list) and give the tail P from foot(L,P). Because i understand well [[A|_]|L], but [A|P] i have some doubts. By the way, always clear. Sorry for my more question on prolog, i m studying the subject.
– theantomc
Jan 2 at 20:19
With[A|P]
we are at that moment constructing (well given we read in in that direction) a list, soA
is the first element of the result, andP
the tail (remaining list). You actually wrote the[A|P]
part yourself in your question.
– Willem Van Onsem
Jan 2 at 20:21
"constructing " you mean a a variable part? (X = [c, h, m]). Because for my first look on the code I thought that this part is refers to list of the list (first element in the query), but i m wrong sure( because is second element)
– theantomc
Jan 2 at 20:28
@theantomc: yes, here we are constructing theX = [c, h, m]
part.
– Willem Van Onsem
Jan 2 at 20:29
add a comment |
maybe this my question will do stupid.. [A|P] in the foo/2 don't take a first list (in list of list) and give the tail P from foot(L,P). Because i understand well [[A|_]|L], but [A|P] i have some doubts. By the way, always clear. Sorry for my more question on prolog, i m studying the subject.
– theantomc
Jan 2 at 20:19
With[A|P]
we are at that moment constructing (well given we read in in that direction) a list, soA
is the first element of the result, andP
the tail (remaining list). You actually wrote the[A|P]
part yourself in your question.
– Willem Van Onsem
Jan 2 at 20:21
"constructing " you mean a a variable part? (X = [c, h, m]). Because for my first look on the code I thought that this part is refers to list of the list (first element in the query), but i m wrong sure( because is second element)
– theantomc
Jan 2 at 20:28
@theantomc: yes, here we are constructing theX = [c, h, m]
part.
– Willem Van Onsem
Jan 2 at 20:29
maybe this my question will do stupid.. [A|P] in the foo/2 don't take a first list (in list of list) and give the tail P from foot(L,P). Because i understand well [[A|_]|L], but [A|P] i have some doubts. By the way, always clear. Sorry for my more question on prolog, i m studying the subject.
– theantomc
Jan 2 at 20:19
maybe this my question will do stupid.. [A|P] in the foo/2 don't take a first list (in list of list) and give the tail P from foot(L,P). Because i understand well [[A|_]|L], but [A|P] i have some doubts. By the way, always clear. Sorry for my more question on prolog, i m studying the subject.
– theantomc
Jan 2 at 20:19
With
[A|P]
we are at that moment constructing (well given we read in in that direction) a list, so A
is the first element of the result, and P
the tail (remaining list). You actually wrote the [A|P]
part yourself in your question.– Willem Van Onsem
Jan 2 at 20:21
With
[A|P]
we are at that moment constructing (well given we read in in that direction) a list, so A
is the first element of the result, and P
the tail (remaining list). You actually wrote the [A|P]
part yourself in your question.– Willem Van Onsem
Jan 2 at 20:21
"constructing " you mean a a variable part? (X = [c, h, m]). Because for my first look on the code I thought that this part is refers to list of the list (first element in the query), but i m wrong sure( because is second element)
– theantomc
Jan 2 at 20:28
"constructing " you mean a a variable part? (X = [c, h, m]). Because for my first look on the code I thought that this part is refers to list of the list (first element in the query), but i m wrong sure( because is second element)
– theantomc
Jan 2 at 20:28
@theantomc: yes, here we are constructing the
X = [c, h, m]
part.– Willem Van Onsem
Jan 2 at 20:29
@theantomc: yes, here we are constructing the
X = [c, h, m]
part.– Willem Van Onsem
Jan 2 at 20:29
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54011157%2ffirst-elements-of-list-of-list-prolog%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown