Code Comments
Programming Forum and web based access to our favorite programming groups.I need a program to sum the length of a list, for example if the list was of length n then the sum would be n-1 + n-2 +...+ n-n+1. Can anyone help?
Post Follow-up to this messageValerie a écrit : > I need a program to sum the length of a list, for example if the list was > of length n then the sum would be n-1 + n-2 +...+ n-n+1. Can anyone help? what about first showing us how you go through recursively an entire list, and then it should be more than obivous how to sum each ead of a list. is it me or this is the end of prolog class in every college in europe ? (this is soon in year, is it ?)
Post Follow-up to this messageValerie wrote: > I need a program to sum the length of a list, for example if the list > was of length n then the sum would be n-1 + n-2 +...+ n-n+1. Can > anyone help? n-n+1 == 1 (n-1) + (n-2) + (n-3) + ... + 3 + 2 + 1 || there are n-1 summands == (n-1+1) + (n-2+2) + (n-3+3) + ... || now there are (n-1)/2 summands == n + n + n + ... || still there are (n-1)/2 summands, all are n == n * (n-1)/2 Mick.
Post Follow-up to this messageValerie wrote: > I need a program to sum the length of a list, for example if the list was > of length n then the sum would be n-1 + n-2 +...+ n-n+1. Can anyone help? Well, as others have pointed out, n-1 + n-2 + ... + (n-(n-1)) = 1 + 2 + ... + n-1 = ((n-1)*n)/2 but I doubt that is what your instructor has in mind. Maybe you should start by describing the function you need to be able to evaluate. Firstly, the function converts (maps) *nonempty* lists into integers, so let's just say value(L,Q) means "the value that the function takes on list L to is Q" in Prolog. Secondly, the value that the function takes on a singleton list is zero by definition. How do you say "the value that the function takes on a singleton list is zero" in Prolog? Now suppose you have a list of the form [X,Y|L] -- i.e., a list that contains two or more elements -- and you know the value that the function takes on [Y|L] is Q1. What has to be added to Q1 to get the value that the function takes in [X,Y|L]? How do you say that in Prolog? Experiment! -- billh
Post Follow-up to this messageValerie wrote: > I need a program to sum the length of a list, for example if the list was > of length n then the sum would be n-1 + n-2 +...+ n-n+1. Can anyone help? Well, as others have pointed out, n-1 + n-2 + ... + (n-(n-1)) = 1 + 2 + ... + n-1 = ((n-1)*n)/2 but I doubt that is what your instructor has in mind. Perhaps you should begin by describing the function that you need to be able to evaluate. Firstly, the function converts (maps) nonempty lists into integers, so let's just say value(L,Q) means "the value that the function takes on nonempty list L to is Q". Secondly, the value that the function takes on a singleton list is zero by definition. So, how do you say "the value that the function takes on a singleton list is zero" in Prolog? Now suppose you have a list of the form [X,Y|L] -- i.e., a list that contains two or more elements -- and you know the value that the function takes on [Y|L] is Q1. What has to be added to Q1 to get the value that the function takes in [X,Y|L]? How do you say that in Prolog? Experiment! -- billh
Post Follow-up to this messageValerie wrote: > I need a program to sum the length of a list, for example if the list was > of length n then the sum would be n-1 + n-2 +...+ n-n+1. Can anyone help? Well, as others have pointed out, n-1 + n-2 + ... + (n-(n-1)) = 1 + 2 + ... + n-1 = ((n-1)*n)/2 but I doubt that is what your instructor has in mind. Perhaps you should begin by describing the function that you need to be able to evaluate. Firstly, the function converts (maps) nonempty lists into integers, so let's just say value(L,Q) means "the value that the function takes on nonempty list L to is Q". Secondly, the value that the function takes on a singleton list is zero by definition. So, how do you say "the value that the function takes on a singleton list is zero" in Prolog? Now suppose you have a list of the form [X,Y|L] -- i.e., a list that contains two or more elements -- and you know the value that the function takes on [Y|L] is Q1. What has to be added to Q1 to get the value that the function takes on [X,Y|L]? How do you say that on Prolog? Experiment! -- billh
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.