Prolog length of a list












3















How can I calculate the length of a list



?- size_sub([[b,a,g], [9,3,7,4], [6]],  X).
X = [3, 4, 1].

?- size_sub([[c,g,e,w], [7]], X).
X = [4, 1].

?- size_sub(, X).
X = .









share|improve this question




















  • 1





    is that last one correct? to be consistent wouldn't that be size_sub([],X). or size_sub(,X) the answer would be

    – Bob Vale
    Sep 15 '11 at 19:50


















3















How can I calculate the length of a list



?- size_sub([[b,a,g], [9,3,7,4], [6]],  X).
X = [3, 4, 1].

?- size_sub([[c,g,e,w], [7]], X).
X = [4, 1].

?- size_sub(, X).
X = .









share|improve this question




















  • 1





    is that last one correct? to be consistent wouldn't that be size_sub([],X). or size_sub(,X) the answer would be

    – Bob Vale
    Sep 15 '11 at 19:50
















3












3








3


0






How can I calculate the length of a list



?- size_sub([[b,a,g], [9,3,7,4], [6]],  X).
X = [3, 4, 1].

?- size_sub([[c,g,e,w], [7]], X).
X = [4, 1].

?- size_sub(, X).
X = .









share|improve this question
















How can I calculate the length of a list



?- size_sub([[b,a,g], [9,3,7,4], [6]],  X).
X = [3, 4, 1].

?- size_sub([[c,g,e,w], [7]], X).
X = [4, 1].

?- size_sub(, X).
X = .






list prolog






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jun 2 '15 at 9:44









repeat

14.7k441136




14.7k441136










asked Sep 15 '11 at 19:45









MaryMary

21113




21113








  • 1





    is that last one correct? to be consistent wouldn't that be size_sub([],X). or size_sub(,X) the answer would be

    – Bob Vale
    Sep 15 '11 at 19:50
















  • 1





    is that last one correct? to be consistent wouldn't that be size_sub([],X). or size_sub(,X) the answer would be

    – Bob Vale
    Sep 15 '11 at 19:50










1




1





is that last one correct? to be consistent wouldn't that be size_sub([],X). or size_sub(,X) the answer would be

– Bob Vale
Sep 15 '11 at 19:50







is that last one correct? to be consistent wouldn't that be size_sub([],X). or size_sub(,X) the answer would be

– Bob Vale
Sep 15 '11 at 19:50














2 Answers
2






active

oldest

votes


















7














To map length/2 over a list of lists, we can use the meta-predicate maplist/3 like this:



size_sub(Xss,Ls):-
maplist(length,Xss,Ls).





share|improve this answer

































    6














    Ok you need to start with the base case which is the last answer



    so size_sub(,X). is true if X= so first you write that as a rule.



    size_sub(,).


    Then you need to do the inductive step a list that is one longer than the previous. I am going to assume that you have a size/2 function for determining the size of a single list (if not please comment).



    So the inductive step is going to operate on the length of the first parameter so N->N+1. We would represent this by striping off the head of the list syntax will be [H|T] now the second parameter (your answer) is going to be the length of H with the result of calling size_sub on T. As we cannot specify rules in the parameters in the header we will use N to represent the length of H and T2 to represent the result of size_sub on T.



    So the first part of the rule becomes size_sub([H|T],[N|T2]):-



    now we follow it with the predicates that will assert the values for N and T2.



    size(H,N),
    size_sub(T,T2).


    putting that all together you get



    size_sub(,).

    size_sub([H|T],[N|T2]):-
    size(H,N),
    size_sub(T,T2).


    size/2 is a far simpler case and following the same process of base + inductive you should be able to create the rules for it. Please comment if you need further help.



    ** EDIT - Request for size/2 definition **



    To define size/2



    Start with the base case, the empty list has a size of 0.



    size(,0).


    Now the inductive step. The size of list of length(N+1) is the size of a list of length(N). So lets define our list as [_|T] I've defined the list using _ to represent the head because we never use it so we can just use the anonymous variable. Lets use N to represent the length of T, and M to be N+1.



    so



    size([_|T],M):-



    now lets define N



      size(T,N),


    and finally assert that M is equal to N + 1



      M is N+1.


    so putting everything together



    size(,0).

    size([_|T],N):-
    size(T,M),
    N is M+1.

    size_sub(,).

    size_sub([H|T],[N|T2]):-
    size(H,N),
    size_sub(T,T2).





    share|improve this answer


























    • I don't know how to create size/2

      – Mary
      Sep 15 '11 at 20:24











    • @Mary answer updated

      – Bob Vale
      Sep 15 '11 at 22:37











    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%2f7436614%2fprolog-length-of-a-list%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    7














    To map length/2 over a list of lists, we can use the meta-predicate maplist/3 like this:



    size_sub(Xss,Ls):-
    maplist(length,Xss,Ls).





    share|improve this answer






























      7














      To map length/2 over a list of lists, we can use the meta-predicate maplist/3 like this:



      size_sub(Xss,Ls):-
      maplist(length,Xss,Ls).





      share|improve this answer




























        7












        7








        7







        To map length/2 over a list of lists, we can use the meta-predicate maplist/3 like this:



        size_sub(Xss,Ls):-
        maplist(length,Xss,Ls).





        share|improve this answer















        To map length/2 over a list of lists, we can use the meta-predicate maplist/3 like this:



        size_sub(Xss,Ls):-
        maplist(length,Xss,Ls).






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited May 23 '17 at 10:27









        Community

        11




        11










        answered Sep 16 '11 at 11:18









        Thanos TintinidisThanos Tintinidis

        5,38511229




        5,38511229

























            6














            Ok you need to start with the base case which is the last answer



            so size_sub(,X). is true if X= so first you write that as a rule.



            size_sub(,).


            Then you need to do the inductive step a list that is one longer than the previous. I am going to assume that you have a size/2 function for determining the size of a single list (if not please comment).



            So the inductive step is going to operate on the length of the first parameter so N->N+1. We would represent this by striping off the head of the list syntax will be [H|T] now the second parameter (your answer) is going to be the length of H with the result of calling size_sub on T. As we cannot specify rules in the parameters in the header we will use N to represent the length of H and T2 to represent the result of size_sub on T.



            So the first part of the rule becomes size_sub([H|T],[N|T2]):-



            now we follow it with the predicates that will assert the values for N and T2.



            size(H,N),
            size_sub(T,T2).


            putting that all together you get



            size_sub(,).

            size_sub([H|T],[N|T2]):-
            size(H,N),
            size_sub(T,T2).


            size/2 is a far simpler case and following the same process of base + inductive you should be able to create the rules for it. Please comment if you need further help.



            ** EDIT - Request for size/2 definition **



            To define size/2



            Start with the base case, the empty list has a size of 0.



            size(,0).


            Now the inductive step. The size of list of length(N+1) is the size of a list of length(N). So lets define our list as [_|T] I've defined the list using _ to represent the head because we never use it so we can just use the anonymous variable. Lets use N to represent the length of T, and M to be N+1.



            so



            size([_|T],M):-



            now lets define N



              size(T,N),


            and finally assert that M is equal to N + 1



              M is N+1.


            so putting everything together



            size(,0).

            size([_|T],N):-
            size(T,M),
            N is M+1.

            size_sub(,).

            size_sub([H|T],[N|T2]):-
            size(H,N),
            size_sub(T,T2).





            share|improve this answer


























            • I don't know how to create size/2

              – Mary
              Sep 15 '11 at 20:24











            • @Mary answer updated

              – Bob Vale
              Sep 15 '11 at 22:37
















            6














            Ok you need to start with the base case which is the last answer



            so size_sub(,X). is true if X= so first you write that as a rule.



            size_sub(,).


            Then you need to do the inductive step a list that is one longer than the previous. I am going to assume that you have a size/2 function for determining the size of a single list (if not please comment).



            So the inductive step is going to operate on the length of the first parameter so N->N+1. We would represent this by striping off the head of the list syntax will be [H|T] now the second parameter (your answer) is going to be the length of H with the result of calling size_sub on T. As we cannot specify rules in the parameters in the header we will use N to represent the length of H and T2 to represent the result of size_sub on T.



            So the first part of the rule becomes size_sub([H|T],[N|T2]):-



            now we follow it with the predicates that will assert the values for N and T2.



            size(H,N),
            size_sub(T,T2).


            putting that all together you get



            size_sub(,).

            size_sub([H|T],[N|T2]):-
            size(H,N),
            size_sub(T,T2).


            size/2 is a far simpler case and following the same process of base + inductive you should be able to create the rules for it. Please comment if you need further help.



            ** EDIT - Request for size/2 definition **



            To define size/2



            Start with the base case, the empty list has a size of 0.



            size(,0).


            Now the inductive step. The size of list of length(N+1) is the size of a list of length(N). So lets define our list as [_|T] I've defined the list using _ to represent the head because we never use it so we can just use the anonymous variable. Lets use N to represent the length of T, and M to be N+1.



            so



            size([_|T],M):-



            now lets define N



              size(T,N),


            and finally assert that M is equal to N + 1



              M is N+1.


            so putting everything together



            size(,0).

            size([_|T],N):-
            size(T,M),
            N is M+1.

            size_sub(,).

            size_sub([H|T],[N|T2]):-
            size(H,N),
            size_sub(T,T2).





            share|improve this answer


























            • I don't know how to create size/2

              – Mary
              Sep 15 '11 at 20:24











            • @Mary answer updated

              – Bob Vale
              Sep 15 '11 at 22:37














            6












            6








            6







            Ok you need to start with the base case which is the last answer



            so size_sub(,X). is true if X= so first you write that as a rule.



            size_sub(,).


            Then you need to do the inductive step a list that is one longer than the previous. I am going to assume that you have a size/2 function for determining the size of a single list (if not please comment).



            So the inductive step is going to operate on the length of the first parameter so N->N+1. We would represent this by striping off the head of the list syntax will be [H|T] now the second parameter (your answer) is going to be the length of H with the result of calling size_sub on T. As we cannot specify rules in the parameters in the header we will use N to represent the length of H and T2 to represent the result of size_sub on T.



            So the first part of the rule becomes size_sub([H|T],[N|T2]):-



            now we follow it with the predicates that will assert the values for N and T2.



            size(H,N),
            size_sub(T,T2).


            putting that all together you get



            size_sub(,).

            size_sub([H|T],[N|T2]):-
            size(H,N),
            size_sub(T,T2).


            size/2 is a far simpler case and following the same process of base + inductive you should be able to create the rules for it. Please comment if you need further help.



            ** EDIT - Request for size/2 definition **



            To define size/2



            Start with the base case, the empty list has a size of 0.



            size(,0).


            Now the inductive step. The size of list of length(N+1) is the size of a list of length(N). So lets define our list as [_|T] I've defined the list using _ to represent the head because we never use it so we can just use the anonymous variable. Lets use N to represent the length of T, and M to be N+1.



            so



            size([_|T],M):-



            now lets define N



              size(T,N),


            and finally assert that M is equal to N + 1



              M is N+1.


            so putting everything together



            size(,0).

            size([_|T],N):-
            size(T,M),
            N is M+1.

            size_sub(,).

            size_sub([H|T],[N|T2]):-
            size(H,N),
            size_sub(T,T2).





            share|improve this answer















            Ok you need to start with the base case which is the last answer



            so size_sub(,X). is true if X= so first you write that as a rule.



            size_sub(,).


            Then you need to do the inductive step a list that is one longer than the previous. I am going to assume that you have a size/2 function for determining the size of a single list (if not please comment).



            So the inductive step is going to operate on the length of the first parameter so N->N+1. We would represent this by striping off the head of the list syntax will be [H|T] now the second parameter (your answer) is going to be the length of H with the result of calling size_sub on T. As we cannot specify rules in the parameters in the header we will use N to represent the length of H and T2 to represent the result of size_sub on T.



            So the first part of the rule becomes size_sub([H|T],[N|T2]):-



            now we follow it with the predicates that will assert the values for N and T2.



            size(H,N),
            size_sub(T,T2).


            putting that all together you get



            size_sub(,).

            size_sub([H|T],[N|T2]):-
            size(H,N),
            size_sub(T,T2).


            size/2 is a far simpler case and following the same process of base + inductive you should be able to create the rules for it. Please comment if you need further help.



            ** EDIT - Request for size/2 definition **



            To define size/2



            Start with the base case, the empty list has a size of 0.



            size(,0).


            Now the inductive step. The size of list of length(N+1) is the size of a list of length(N). So lets define our list as [_|T] I've defined the list using _ to represent the head because we never use it so we can just use the anonymous variable. Lets use N to represent the length of T, and M to be N+1.



            so



            size([_|T],M):-



            now lets define N



              size(T,N),


            and finally assert that M is equal to N + 1



              M is N+1.


            so putting everything together



            size(,0).

            size([_|T],N):-
            size(T,M),
            N is M+1.

            size_sub(,).

            size_sub([H|T],[N|T2]):-
            size(H,N),
            size_sub(T,T2).






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Sep 15 '11 at 20:34

























            answered Sep 15 '11 at 20:10









            Bob ValeBob Vale

            15.7k2742




            15.7k2742













            • I don't know how to create size/2

              – Mary
              Sep 15 '11 at 20:24











            • @Mary answer updated

              – Bob Vale
              Sep 15 '11 at 22:37



















            • I don't know how to create size/2

              – Mary
              Sep 15 '11 at 20:24











            • @Mary answer updated

              – Bob Vale
              Sep 15 '11 at 22:37

















            I don't know how to create size/2

            – Mary
            Sep 15 '11 at 20:24





            I don't know how to create size/2

            – Mary
            Sep 15 '11 at 20:24













            @Mary answer updated

            – Bob Vale
            Sep 15 '11 at 22:37





            @Mary answer updated

            – Bob Vale
            Sep 15 '11 at 22:37


















            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%2f7436614%2fprolog-length-of-a-list%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

            Mossoró

            Error while reading .h5 file using the rhdf5 package in R

            Pushsharp Apns notification error: 'InvalidToken'