For Programmers: Free Programming Magazines  


Home > Archive > Prolog > March 2005 > sum the length of a list..









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author sum the length of a list..
Valerie

2005-03-08, 4:00 pm

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?
Djamé Seddah

2005-03-08, 4:00 pm

Valerie 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 ?)
Mick Krippendorf

2005-03-08, 4:00 pm

Valerie 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.
student

2005-03-09, 3:59 am

Valerie 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
coyote

2005-03-09, 3:59 am

Valerie 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



student

2005-03-09, 3:59 am

Valerie 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


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com