First elements of list of list Prolog












1















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?










share|improve this question





























    1















    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?










    share|improve this question



























      1












      1








      1








      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?










      share|improve this question
















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 3 at 2:50









      false

      10.2k773150




      10.2k773150










      asked Jan 2 at 18:10









      theantomctheantomc

      1209




      1209
























          1 Answer
          1






          active

          oldest

          votes


















          2














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





          share|improve this answer


























          • 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











          • "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











          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
          });


          }
          });














          draft saved

          draft discarded


















          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









          2














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





          share|improve this answer


























          • 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











          • "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
















          2














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





          share|improve this answer


























          • 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











          • "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














          2












          2








          2







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





          share|improve this answer















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






          share|improve this answer














          share|improve this answer



          share|improve this answer








          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, 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











          • @theantomc: yes, here we are constructing the X = [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











          • 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











          • @theantomc: yes, here we are constructing the X = [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




















          draft saved

          draft discarded




















































          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.




          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          Angular Downloading a file using contenturl with Basic Authentication

          Olmecas

          Can't read property showImagePicker of undefined in react native iOS