From: Mario Lang
Subject: Sum a list
Date: 
Message-ID: <87bswl54f9.fsf@home.delysid.org>
Hi.

I know this is probably very trivial, but I cant find anything about it:

How can I sum the numbers in a list.
I have a function, which returns a list like this:

(3/4 3/4 3/4 3/4)

I want to know what all those values are together. So I tried:
(+ (funcname))

But the interpreter told me something like function + expects a number.

I see why, but I dont know how to "unlistify" those values.

-- 
Thanks,
   Mario <·····@delysid.org>
Homepage(s): http://delysid.org | http://piss.at/

It's now the GNU Emacs of all terminal emulators.
	-- Linus Torvalds, regarding the fact that Linux started off as a terminal emulator

From: Friedrich Dominicus
Subject: Re: Sum a list
Date: 
Message-ID: <87zok5ndmx.fsf@q-software-solutions.com>
Mario Lang <·····@home.delysid.org> writes:

> Hi.
> 
> I know this is probably very trivial, but I cant find anything about it:
> 
> How can I sum the numbers in a list.
> I have a function, which returns a list like this:
> 
> (3/4 3/4 3/4 3/4)
> 
> I want to know what all those values are together. So I tried:
> (+ (funcname))
You can try: 

(apply '+ (funcname))

Regards
Friedrich

-- 
for e-mail reply remove all after .com 
From: Mario Lang
Subject: Re: Sum a list
Date: 
Message-ID: <8766ms6gtq.fsf@home.delysid.org>
Friedrich Dominicus <·····@q-software-solutions.com> writes:

> Mario Lang <·····@home.delysid.org> writes:
> 
> > I know this is probably very trivial, but I cant find anything about it:
> > 
> > How can I sum the numbers in a list.
> > I have a function, which returns a list like this:
> > 
> > (3/4 3/4 3/4 3/4)
> > 
> > I want to know what all those values are together. So I tried:
> > (+ (funcname))
> You can try: 
> 
> (apply '+ (funcname))
Thanks alot. It works.

> Regards
> Friedrich
> 
> -- 
> for e-mail reply remove all after .com 

-- 
CYa,
   Mario <·····@delysid.org>
Homepage(s): http://delysid.org | http://piss.at/

RAM wasn't built in a day.
From: Pierre R. Mai
Subject: Re: Sum a list
Date: 
Message-ID: <87og0kk54k.fsf@orion.bln.pmsf.de>
Mario Lang <·····@home.delysid.org> writes:

> Friedrich Dominicus <·····@q-software-solutions.com> writes:
> 
> > Mario Lang <·····@home.delysid.org> writes:
> > 
> > > I know this is probably very trivial, but I cant find anything about it:
> > > 
> > > How can I sum the numbers in a list.
> > > I have a function, which returns a list like this:
> > > 
> > > (3/4 3/4 3/4 3/4)
> > > 
> > > I want to know what all those values are together. So I tried:
> > > (+ (funcname))
> > You can try: 
> > 
> > (apply '+ (funcname))
> Thanks alot. It works.

It works, but it is the wrong solution, unless you can guarantee that
the number of elements in the list is bounded by a small integer less
than or equal to 50, because + will be called with all the elements of
the list as arguments, which may be more than call-arguments-limit of
the implementation you use.  It may also be inefficient, even if it
does work.

What you really want to use in this case is reduce, i.e.:

(reduce #'+ (funcname))

reduce will call + with only 0 or 2 arguments at a time, thereby
working correctly for sequences > 50, too.

Regs, Pierre.

-- 
Pierre R. Mai <····@acm.org>                    http://www.pmsf.de/pmai/
 The most likely way for the world to be destroyed, most experts agree,
 is by accident. That's where we come in; we're computer professionals.
 We cause accidents.                           -- Nathaniel Borenstein
From: Paul Dietz
Subject: Re: Sum a list
Date: 
Message-ID: <8shl7f$loj$1@schbbs.mot.com>
In article <··············@orion.bln.pmsf.de>,
Pierre R. Mai <····@acm.org> wrote:

> What you really want to use in this case is reduce, i.e.:
>
> (reduce #'+ (funcname))

Naw, what you really want is

   (loop for x in (funcname) sum x)

But then I grew up on Interlisp. :)

	Paul
From: Rainer Joswig
Subject: Re: Sum a list
Date: 
Message-ID: <joswig-8D90C9.16381717102000@news.is-europe.net>
In article <············@schbbs.mot.com>, ·····@comm.mot.com (Paul 
Dietz) wrote:

> In article <··············@orion.bln.pmsf.de>,
> Pierre R. Mai <····@acm.org> wrote:
> 
> > What you really want to use in this case is reduce, i.e.:
> >
> > (reduce #'+ (funcname))
> 
> Naw, what you really want is
> 
>    (loop for x in (funcname) sum x)
> 
> But then I grew up on Interlisp. :)
> 
> 	Paul
> 

You are so "imperative"... ;-)

(series:collect-sum
 (series:scan (list 1 2 3)))

-- 
Rainer Joswig, Hamburg, Germany
Email: ·············@corporate-world.lisp.de
Web: http://corporate-world.lisp.de/
From: Paul Rudin
Subject: Re: Sum a list
Date: 
Message-ID: <m3itqt3p0n.fsf@cara.scientia.com>
>>>>> "Mario" == Mario Lang <·····@home.delysid.org> writes:

 Mario> Hi.
 Mario> I know this is probably very trivial, but I cant find anything about it:

 Mario> How can I sum the numbers in a list.
 Mario> I have a function, which returns a list like this:

 Mario> (3/4 3/4 3/4 3/4)

 Mario> I want to know what all those values are together. So I tried:
 Mario> (+ (funcname))

 Mario> But the interpreter told me something like function + expects a number.

 Mario> I see why, but I dont know how to "unlistify" those values.


If you know that the length of the list will be less than the value of
CALL-ARGUMENTS-LIMIT in any CL implementations you want to run this
code with then you can use

 (apply #'+ (funcname))

otherwise you could use

 (reduce #'+ (funcname))
From: Pierre R. Mai
Subject: Re: Sum a list
Date: 
Message-ID: <87lmvok4h1.fsf@orion.bln.pmsf.de>
Paul Rudin <··········@scientia.com> writes:

> If you know that the length of the list will be less than the value of
> CALL-ARGUMENTS-LIMIT in any CL implementations you want to run this
> code with then you can use
> 
>  (apply #'+ (funcname))
> 
> otherwise you could use
> 
>  (reduce #'+ (funcname))

I also think that there is a semantic difference that is being
communicated by the use of reduce vs. apply:

Use of apply indicates that the list was really intended as an
argument-list in the first place, whereas reduce indicates that the
list is a sequence of data items, e.g. it could just as well have been
a vector instead (which is why reduce works on sequences, whereas
apply doesn't ...).

Which is why I'd use reduce in preference to apply in the case at
hand, regardless of the lowest bound on call-arguments-limit, which is
50 in theory, and as low as 64 or 255 in practice (*kcl and
derivatives, like gcl, ecl, ecls, etc. and Symbolics).

Regs, Pierre.

-- 
Pierre R. Mai <····@acm.org>                    http://www.pmsf.de/pmai/
 The most likely way for the world to be destroyed, most experts agree,
 is by accident. That's where we come in; we're computer professionals.
 We cause accidents.                           -- Nathaniel Borenstein
From: Paul Rudin
Subject: Re: Sum a list
Date: 
Message-ID: <wkhf6c1m7w.fsf@ntlworld.com>
>>>>> "Pierre" == Pierre R Mai <····@acm.org> writes:

 Pierre> Paul Rudin <··········@scientia.com> writes:
 >> If you know that the length of the list will be less than the
 >> value of CALL-ARGUMENTS-LIMIT in any CL implementations you want
 >> to run this code with then you can use
 >> 
 >> (apply #'+ (funcname))
 >> 
 >> otherwise you could use
 >> 
 >> (reduce #'+ (funcname))

 Pierre> I also think that there is a semantic difference that is
 Pierre> being communicated by the use of reduce vs. apply:

 Pierre> Use of apply indicates that the list was really intended as
 Pierre> an argument-list in the first place, whereas reduce indicates
 Pierre> that the list is a sequence of data items, e.g. it could just
 Pierre> as well have been a vector instead (which is why reduce works
 Pierre> on sequences, whereas apply doesn't ...).


I'd agree with that but presumably you can have a reasonable
expectation that

  (apply #'+ ...) 

will be faster than 

  (reduce #'+ ...)

?
From: Johan Kullstam
Subject: Re: Sum a list
Date: 
Message-ID: <m2snpwp8dx.fsf@euler.axel.nom>
Paul Rudin <··········@ntlworld.com> writes:

> >>>>> "Pierre" == Pierre R Mai <····@acm.org> writes:
> 
>  Pierre> Paul Rudin <··········@scientia.com> writes:
>  >> If you know that the length of the list will be less than the
>  >> value of CALL-ARGUMENTS-LIMIT in any CL implementations you want
>  >> to run this code with then you can use
>  >> 
>  >> (apply #'+ (funcname))
>  >> 
>  >> otherwise you could use
>  >> 
>  >> (reduce #'+ (funcname))
> 
>  Pierre> I also think that there is a semantic difference that is
>  Pierre> being communicated by the use of reduce vs. apply:
> 
>  Pierre> Use of apply indicates that the list was really intended as
>  Pierre> an argument-list in the first place, whereas reduce indicates
>  Pierre> that the list is a sequence of data items, e.g. it could just
>  Pierre> as well have been a vector instead (which is why reduce works
>  Pierre> on sequences, whereas apply doesn't ...).
> 
> 
> I'd agree with that but presumably you can have a reasonable
> expectation that
> 
>   (apply #'+ ...) 
> 
> will be faster than 
> 
>   (reduce #'+ ...)
> 
> ?

why would you expect that?  i figured that REDUCE #'+ was a speed
optimization over APPLY #'+.  either way you have to sum pairs of
numbers (since most processors do not have multi-way addition
op-codes) to get the total.  REDUCE just runs #'+ over the list, but
APPLY would call #'+ with all those arguments only to find out that
#'+ on a list is really REDUCE #'+.

-- 
J o h a n  K u l l s t a m
[········@ne.mediaone.net]
Don't Fear the Penguin!
From: Christopher Browne
Subject: Re: Sum a list
Date: 
Message-ID: <slrn8up12u.uq.cbbrowne@knuth.brownes.org>
In our last episode (Tue, 17 Oct 2000 01:22:29 GMT),
the artist formerly known as Johan Kullstam said:
>Paul Rudin <··········@ntlworld.com> writes:
>
>> >>>>> "Pierre" == Pierre R Mai <····@acm.org> writes:
>> 
>>  Pierre> Paul Rudin <··········@scientia.com> writes:
>>  >> If you know that the length of the list will be less than the
>>  >> value of CALL-ARGUMENTS-LIMIT in any CL implementations you want
>>  >> to run this code with then you can use
>>  >> 
>>  >> (apply #'+ (funcname))
>>  >> 
>>  >> otherwise you could use
>>  >> 
>>  >> (reduce #'+ (funcname))
>> 
>>  Pierre> I also think that there is a semantic difference that is
>>  Pierre> being communicated by the use of reduce vs. apply:
>> 
>>  Pierre> Use of apply indicates that the list was really intended as
>>  Pierre> an argument-list in the first place, whereas reduce indicates
>>  Pierre> that the list is a sequence of data items, e.g. it could just
>>  Pierre> as well have been a vector instead (which is why reduce works
>>  Pierre> on sequences, whereas apply doesn't ...).
>> 
>> 
>> I'd agree with that but presumably you can have a reasonable
>> expectation that
>> 
>>   (apply #'+ ...) 
>> 
>> will be faster than 
>> 
>>   (reduce #'+ ...)
>> 
>> ?
>
>why would you expect that?  i figured that REDUCE #'+ was a speed
>optimization over APPLY #'+.  either way you have to sum pairs of
>numbers (since most processors do not have multi-way addition
>op-codes) to get the total.  REDUCE just runs #'+ over the list, but
>APPLY would call #'+ with all those arguments only to find out that
>#'+ on a list is really REDUCE #'+.

I could imagine this going either way.

On the one hand, REDUCE indicates a serial ordering, whereas APPLY
doesn't.  This is likely not to matter much for addition.

Supposing (APPLY #'+ somelist) "expands" to (REDUCE #'+ somelist), then
it might be a little bit (O(1)) faster, which gets less significant the
bigger the list...

I'd worry about the semantics here, not the speed; I'd not expect any
significant difference in speed, and correctness surely wins...
-- 
········@acm.org - <http://www.hex.net/~cbbrowne/lsf.html>
When sign makers go on strike, is anything written on their signs? 
From: Paul Rudin
Subject: Re: Sum a list
Date: 
Message-ID: <wky9zo3qod.fsf@ntlworld.com>
>>>>> "Johan" == Johan Kullstam <········@ne.mediaone.net> writes:


 Johan> why would you expect that?  i figured that REDUCE #'+ was a
 Johan> speed optimization over APPLY #'+.  either way you have to sum
 Johan> pairs of numbers (since most processors do not have multi-way
 Johan> addition op-codes) to get the total.  REDUCE just runs #'+
 Johan> over the list, but APPLY would call #'+ with all those
 Johan> arguments only to find out that #'+ on a list is really REDUCE
 Johan> #'+.

In general reduce is required to funcall the function on pairwise
elements and intermediate results down the list. OTOH you can imagine
that compiler writers could find ways of significantly improving the
behaviour of #'+ compared with this. (Although I've no idea if this
is indeed the case.)
From: Patrick A. O'Donnell
Subject: Re: Sum a list
Date: 
Message-ID: <rt4s2893r7.fsf@ascent.com>
Paul Rudin <··········@ntlworld.com> writes:
> I'd agree with that but presumably you can have a reasonable
> expectation that
> 
>   (apply #'+ ...) 
> 
> will be faster than 
> 
>   (reduce #'+ ...)
> 
> ?

Not at all.  I can easily imagine a compiler writer open-coding either
or both versions.  Given an assumption (whether or not well-grounded)
of using reduce more commonly in computations and apply less commonly,
this compiler writer may be more inclined to optimize the reduce.

		- Pat
From: Christopher Browne
Subject: Re: Sum a list
Date: 
Message-ID: <slrn8unru1.3vuagvv.cbbrowne@test.hex.net>
In our last episode (Mon, 16 Oct 2000 12:57:45 GMT),
the artist formerly known as Mario Lang said:
>I know this is probably very trivial, but I cant find anything about it:
>
>How can I sum the numbers in a list.
>I have a function, which returns a list like this:
>
>(3/4 3/4 3/4 3/4)
>
>I want to know what all those values are together. So I tried:
>(+ (funcname))
>
>But the interpreter told me something like function + expects a number.
>
>I see why, but I dont know how to "unlistify" those values.

Two options leap to mind:

a) Use the reduce function.  (Visions of APL dancing in their
heads...)

(reduce #'+ '(3/4 3/4 3/4 3/4))
3

b) Use apply.

(apply #'+ '(3/4 3/4 3/4 3/4))

These are doing fairly different things; reduce is essentially
interposing #'+ in its role as a binary operator _in between_ the
values, whereas apply is using #'+ to add up the whole list.
-- 
(concatenate 'string "aa454" ·@" "freenet.carleton.ca")
<http://www.ntlug.org/~cbbrowne/lisp.html>
"I  worry that  the person  who thought  up Muzak  may be  thinking up
something else." -- Lily Tomlin