Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

sum the length of a list..
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?

Report this thread to moderator Post Follow-up to this message
Old Post
Valerie
03-08-05 09:00 PM


Re: sum the length of a list..
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 ?)

Report this thread to moderator Post Follow-up to this message
Old Post
Djamé Seddah
03-08-05 09:00 PM


Re: sum the length of a list..
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.

Report this thread to moderator Post Follow-up to this message
Old Post
Mick Krippendorf
03-08-05 09:00 PM


Re: sum the length of a list..
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

Report this thread to moderator Post Follow-up to this message
Old Post
student
03-09-05 08:59 AM


Re: sum the length of a list..
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




Report this thread to moderator Post Follow-up to this message
Old Post
coyote
03-09-05 08:59 AM


Re: sum the length of a list..
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



Report this thread to moderator Post Follow-up to this message
Old Post
student
03-09-05 08:59 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Prolog archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 08:02 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.