From: Darrell Adams
Subject: How to sum a list?
Date: 
Message-ID: <0YKdncr0oOWOZHXeRVn-tA@comcast.com>
I am very new to LISP.

I would like to know how to return the sum, over all the elements, of a 
variable length list.

I know that you can use

(+ 1 2 3 ...) to get a sum of a few items.

If I have an item of variable length as in

(setq vector '(1 2 3 ... n))

I would like to have the sum returned.

I hope this is clear enough.

Thanks,
Darrell

From: funkyj
Subject: Re: How to sum a list?
Date: 
Message-ID: <1139346355.387263.24660@g14g2000cwa.googlegroups.com>
> (setq vector '(1 2 3 ...))


   REPL> (apply #'+ vector)

would do the trick.

"REPL>" above represents an interactive lisp prompt.  If you are using
"clisp" then the REPL prompt will be "[1]>" when you first start clisp.

a note on style:  Common Lisp has a data type named "vector".  creating
a variable named "vector" that holds a list might be too clever by
half.  You might continue the "iceland/greenland" theme like this:

  (defvar list #(1 2 3))  ; variable 'list' containing a vector
  (defvar vector (make-hash-table))  ; vector contains a hash-table
  (defvar hash-table '(5 6 7)) ; hash-table contains a list.

Cheers,
  --jfc
From: Darrell Adams
Subject: Re: How to sum a list?
Date: 
Message-ID: <cvednVT4FvKcknTenZ2dnUVZ_vidnZ2d@comcast.com>
Yes, your code works as well.  What would be the difference in using

(apply #' ...) versus (reduce #' ...) as Pascal suggested?  To me they both
appear to do the same thing.

funkyj wrote:

>> (setq vector '(1 2 3 ...))
> 
> 
>    REPL> (apply #'+ vector)
> 
> would do the trick.
> 
> "REPL>" above represents an interactive lisp prompt.  If you are using
> "clisp" then the REPL prompt will be "[1]>" when you first start clisp.
> 
> a note on style:  Common Lisp has a data type named "vector".  creating
> a variable named "vector" that holds a list might be too clever by
> half.  You might continue the "iceland/greenland" theme like this:
> 
>   (defvar list #(1 2 3))  ; variable 'list' containing a vector
>   (defvar vector (make-hash-table))  ; vector contains a hash-table
>   (defvar hash-table '(5 6 7)) ; hash-table contains a list.
> 
> Cheers,
>   --jfc
From: funkyj
Subject: Re: How to sum a list?
Date: 
Message-ID: <1139370539.275354.235430@z14g2000cwz.googlegroups.com>
after playing with reduce for a few moments I discovered the important
difference between reduce and apply: reduce will work on a list or a
vector.  e.g. these both work

  REPL> (reduce #'+ #(1 2 3 4))
  REPL> (reduce #'+ '(1 2 3 4))

while apply only works on a list:

  REPL> (apply #'+ #(1 2 3 4))  ; error here.

The folding mentioned by others may have performance implications (I
haven't tested this).

Cheers,
  --jfc
From: Ulrich Hobelmann
Subject: Re: How to sum a list?
Date: 
Message-ID: <44snvgF3rht1U1@individual.net>
Darrell Adams wrote:
> Yes, your code works as well.  What would be the difference in using
> 
> (apply #' ...) versus (reduce #' ...) as Pascal suggested?  To me they both
> appear to do the same thing.

Apply calls the function with its given list as arguments, so it 
basically results in calling (+ 1 2 3 ...), but as someone mentioned, 
the argument list length might be limited.

REDUCE is a fold operation that calls its (binary) function argument on 
two list values, then calls it again on the result and the next list 
value etc.

In infix notation: 1 + 2 + 3 ...
or
(+ (+ 1 2) 3)...

-- 
Suffering from Gates-induced brain leakage...
From: ·······@gmail.com
Subject: Re: How to sum a list?
Date: 
Message-ID: <1139354393.759162.276580@g44g2000cwa.googlegroups.com>
(apply #'+ '(1 2 3)) is equivalent to (+ 1 2 3)
(reduce #'+ '(1 2 3)) is equivalent to (+ (+ 1 2) 3)

REPL> lambda-parameters-limit
4096

This means (apply #'+ '(1 2 3 4 . . . 4097)) causes an error.  So if
you are summing a list of more than 4096 elements (i dunno why you'd
want to), apply  is not an option.
From: Geoffrey Summerhayes
Subject: Re: How to sum a list?
Date: 
Message-ID: <cbbGf.3023$J%6.191194@news20.bellglobal.com>
<·······@gmail.com> wrote in message ·····························@g44g2000cwa.googlegroups.com...
> (apply #'+ '(1 2 3)) is equivalent to (+ 1 2 3)
> (reduce #'+ '(1 2 3)) is equivalent to (+ (+ 1 2) 3)
>
> REPL> lambda-parameters-limit
> 4096

My mistake. The constant in question should be
CALL-ARGUMENTS-LIMIT. In any case, for portability
the smallest number allowed by the ANSI spec. is
only 50

LW Personal 4.4.6 gives 255 for both.

> This means (apply #'+ '(1 2 3 4 . . . 4097)) causes an error.  So if
> you are summing a list of more than 4096 elements (i dunno why you'd
> want to), apply  is not an option.

REDUCE is simpler than playing it safe with:

(defun sum-list(list)
  (if (> (length list)
         call-arguments-limit)
      (reduce #'+ list)
    (apply #'+ list)))

---
Geoff
From: ·······@gmail.com
Subject: Re: How to sum a list?
Date: 
Message-ID: <1139370275.791238.233940@f14g2000cwb.googlegroups.com>
Thanks Geoff, I just lifted your constant without double-checking.  My
bad.
From: Darrell Adams
Subject: Re: How to sum a list?
Date: 
Message-ID: <s6mdnbw70bSQ2XTeRVn-tA@comcast.com>
Ulrich Hobelmann wrote:

> Darrell Adams wrote:
>> Yes, your code works as well.  What would be the difference in using
>> 
>> (apply #' ...) versus (reduce #' ...) as Pascal suggested?  To me they
>> both appear to do the same thing.
> 
> Apply calls the function with its given list as arguments, so it
> basically results in calling (+ 1 2 3 ...), but as someone mentioned,
> the argument list length might be limited.
> 
> REDUCE is a fold operation that calls its (binary) function argument on
> two list values, then calls it again on the result and the next list
> value etc.
> 
> In infix notation: 1 + 2 + 3 ...
> or
> (+ (+ 1 2) 3)...
> 

That works for me.  Thanks for the info and help.
From: David Sletten
Subject: Re: How to sum a list?
Date: 
Message-ID: <3oaGf.5615$Z3.2103@tornado.socal.rr.com>
Ulrich Hobelmann wrote:

> 
> REDUCE is a fold operation that calls its (binary) function argument on 
> two list values, then calls it again on the result and the next list 
> value etc.
> 
> In infix notation: 1 + 2 + 3 ...
> or
> (+ (+ 1 2) 3)...
> 

The default is to fold left. You can also fold right, which can make a 
difference:
(reduce #'- '(5 6 7 8 9)) => ((((5 - 6) - 7) - 8) -9) => -25
(reduce #'- '(5 6 7 8 9) :from-end t) => (5 - (6 - (7 - (8 - 9)))) => 7

Aloha,
David Sletten
From: ·········@cern.ch
Subject: Re: How to sum a list?
Date: 
Message-ID: <yzohd7acenv.fsf@cern.ch>
>>>>> "Ulrich" == Ulrich Hobelmann <···········@web.de> writes:

>> Yes, your code works as well.  What would be the difference in using
>> (apply #' ...) versus (reduce #' ...) as Pascal suggested?  To me
>> they both
>> appear to do the same thing.

Ulrich> Apply calls the function with its given list as arguments, so it
Ulrich> basically results in calling (+ 1 2 3 ...), but as someone mentioned,
Ulrich> the argument list length might be limited.

Ulrich> REDUCE is a fold operation that calls its (binary) function argument
Ulrich> on two list values, then calls it again on the result and the next
Ulrich> list value etc.

*And* REDUCE accepts a general sequence - so it would work with a vector.

CL-USER> (reduce #'+ #(1 2 3))
6

Ole
From: Ivan Boldyrev
Subject: Re: How to sum a list?
Date: 
Message-ID: <q6pob3-4b7.ln1@ibhome.cgitftp.uiggm.nsc.ru>
On 9379 day of my life Darrell Adams wrote:
> What would be the difference in using
>
> (apply #' ...) versus (reduce #' ...) as Pascal suggested?  To me they both
> appear to do the same thing.

[1]> (reduce #'+ (make-list (1+ call-arguments-limit) :initial-element 3))
12291      ; For example

[2]> (apply #'+ (make-list (1+ call-arguments-limit) :initial-element 3))

*** - APPLY: too many arguments given to +
The following restarts are available:
ABORT          :R1      ABORT


-- 
Ivan Boldyrev

                      Ok people, move along, there's nothing to see here.
From: Robert Uhl
Subject: Re: How to sum a list?
Date: 
Message-ID: <m37j866evt.fsf@NOSPAMgmail.com>
Darrell Adams <·········@gmail.com> writes:

> I am very new to LISP.
>
> I would like to know how to return the sum, over all the elements, of a
> variable length list.
>
> I know that you can use
>
> (+ 1 2 3 ...) to get a sum of a few items.

Yes, that works because #'+ is a function which can take a variable
number of arguments.

> If I have an item of variable length as in
>
> (setq vector '(1 2 3 ... n))
>
> I would like to have the sum returned.

You can use APPLY to apply a function to a list:

  (apply #'+ vector)

-- 
Robert Uhl <http://public.xdi.org/=ruhl>
When you disarm your subjects you offend them by showing that either
from cowardliness or lack of faith, you distrust them; and either
conclusion will induce them to hate you.
      --Niccolo Machiavelli, The Prince
From: Geoffrey King
Subject: Re: How to sum a list?
Date: 
Message-ID: <43e8fa40$0$32665$afc38c87@news.optusnet.com.au>
(apply #'+ '( 1 2 3))

Darrell Adams wrote:
> I am very new to LISP.
> 
> I would like to know how to return the sum, over all the elements, of a 
> variable length list.
> 
> I know that you can use
> 
> (+ 1 2 3 ...) to get a sum of a few items.
> 
> If I have an item of variable length as in
> 
> (setq vector '(1 2 3 ... n))
> 
> I would like to have the sum returned.
> 
> I hope this is clear enough.
> 
> Thanks,
> Darrell
From: Geoffrey Summerhayes
Subject: Re: How to sum a list?
Date: 
Message-ID: <1139342584.207585.56220@o13g2000cwo.googlegroups.com>
Geoffrey King <··············@optushome.com.au> wrote:
>
> (apply #'+ '( 1 2 3))
>

Safer, due to LAMBDA-PARAMETERS-LIMIT, is:

(reduce #'+ '(1 2 3) :initial-value 0)

--
Geoff
From: Geoffrey King
Subject: Re: How to sum a list?
Date: 
Message-ID: <43e8fd98$0$15128$afc38c87@news.optusnet.com.au>
Why the ":initial-value 0" ?

Geoffrey Summerhayes wrote:
> Geoffrey King <··············@optushome.com.au> wrote:
>> (apply #'+ '( 1 2 3))
>>
> 
> Safer, due to LAMBDA-PARAMETERS-LIMIT, is:
> 
> (reduce #'+ '(1 2 3) :initial-value 0)
> 
> --
> Geoff
> 
From: Geoffrey Summerhayes
Subject: Re: How to sum a list?
Date: 
Message-ID: <1139344860.812501.273940@g44g2000cwa.googlegroups.com>
Geoffrey King <··············@optushome.com.au> wrote:

> Why the ":initial-value 0" ?

Force of habit. :-)

P.S. I also over-parenthesize infix math in C/C++.

--
Geoff
From: Kenny Tilton
Subject: Re: How to sum a list?
Date: 
Message-ID: <yu7Gf.2495$lG2.51@news-wrt-01.rdc-nyc.rr.com>
Geoffrey King wrote:
> (apply #'+ '( 1 2 3))

True enough, but the OP asked about a vector.

(loop for x across #(1 2 3) summing x)

?

ken

> 
> Darrell Adams wrote:
> 
>> I am very new to LISP.
>>
>> I would like to know how to return the sum, over all the elements, of 
>> a variable length list.
>>
>> I know that you can use
>>
>> (+ 1 2 3 ...) to get a sum of a few items.
>>
>> If I have an item of variable length as in
>>
>> (setq vector '(1 2 3 ... n))
>>
>> I would like to have the sum returned.
>>
>> I hope this is clear enough.
>>
>> Thanks,
>> Darrell
From: Gareth McCaughan
Subject: Re: How to sum a list?
Date: 
Message-ID: <87d5hy214y.fsf@g.mccaughan.ntlworld.com>
Kenny Tilton wrote:

> Geoffrey King wrote:
>> (apply #'+ '( 1 2 3))
> 
> True enough, but the OP asked about a vector.

No, he asked about a list that he'd put in a variable called VECTOR. :-)

-- 
Gareth McCaughan
.sig under construc
From: Pascal Bourguignon
Subject: Re: How to sum a list?
Date: 
Message-ID: <871wyeoh6w.fsf@thalassa.informatimago.com>
Gareth McCaughan <················@pobox.com> writes:

> Kenny Tilton wrote:
>
>> Geoffrey King wrote:
>>> (apply #'+ '( 1 2 3))
>> 
>> True enough, but the OP asked about a vector.
>
> No, he asked about a list that he'd put in a variable called VECTOR. :-)

OPs are twisted.

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

"You can tell the Lisp programmers.  They have pockets full of punch
 cards with close parentheses on them." --> http://tinyurl.com/8ubpf
From: Kenny Tilton
Subject: Re: How to sum a list?
Date: 
Message-ID: <Y1dGf.2050$Lp2.762@news-wrt-01.rdc-nyc.rr.com>
Pascal Bourguignon wrote:
> Gareth McCaughan <················@pobox.com> writes:
> 
> 
>>Kenny Tilton wrote:
>>
>>
>>>Geoffrey King wrote:
>>>
>>>>(apply #'+ '( 1 2 3))
>>>
>>>True enough, but the OP asked about a vector.
>>
>>No, he asked about a list that he'd put in a variable called VECTOR. :-)
> 
> 
> OPs are twisted.
> 

And because he said VECTOR I /saw/ #! That is so cool.

:)

kenny
From: Duane Rettig
Subject: Re: How to sum a list?
Date: 
Message-ID: <o0irrq8ex3.fsf@franz.com>
Kenny Tilton <·············@nyc.rr.com> writes:

> Pascal Bourguignon wrote:
>> Gareth McCaughan <················@pobox.com> writes:
>>
>>>Kenny Tilton wrote:
>>>
>>>
>>>>Geoffrey King wrote:
>>>>
>>>>>(apply #'+ '( 1 2 3))
>>>>
>>>>True enough, but the OP asked about a vector.
>>>
>>>No, he asked about a list that he'd put in a variable called VECTOR. :-)
>> OPs are twisted.
>>
>
> And because he said VECTOR I /saw/ #! That is so cool.
>
> :)

It's because as a lisper, you don't see the syntax!

-- 
Duane Rettig    ·····@franz.com    Franz Inc.  http://www.franz.com/
555 12th St., Suite 1450               http://www.555citycenter.com/
Oakland, Ca. 94607        Phone: (510) 452-2000; Fax: (510) 452-0182   
From: André Thieme
Subject: Re: How to sum a list?
Date: 
Message-ID: <1139391211.706329.280210@g44g2000cwa.googlegroups.com>
Duane Rettig schrieb:

> It's because as a lisper, you don't see the syntax!


I was *so* certain that I saw the #.
Funny phenomenon.


André
--
From: Pascal Bourguignon
Subject: Re: How to sum a list?
Date: 
Message-ID: <87d5hym40z.fsf@thalassa.informatimago.com>
"Andr� Thieme" <······························@justmail.de> writes:

> Duane Rettig schrieb:
>
>> It's because as a lisper, you don't see the syntax!
>
>
> I was *so* certain that I saw the #.
> Funny phenomenon.

I didn't see the variable name! :-)

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

WARNING: This product warps space and time in its vicinity.
From: Coby Beck
Subject: Re: How to sum a list?
Date: 
Message-ID: <qvPGf.182385$6K2.45011@edtnps90>
"Pascal Bourguignon" <······@informatimago.com> wrote in message 
···················@thalassa.informatimago.com...
> "Andr� Thieme" <······························@justmail.de> writes:
>
>> Duane Rettig schrieb:
>>
>>> It's because as a lisper, you don't see the syntax!
>>
>>
>> I was *so* certain that I saw the #.
>> Funny phenomenon.
>
> I didn't see the variable name! :-)

I didn't even see the post, I just suddenly spoke aloud the answer!

(Ha, top that!)

-- 
Coby Beck
(remove #\Space "coby 101 @ bigpond . com")
From: Kenny Tilton
Subject: Re: How to sum a list?
Date: 
Message-ID: <ORsGf.2996$lG2.2936@news-wrt-01.rdc-nyc.rr.com>
Duane Rettig wrote:
> Kenny Tilton <·············@nyc.rr.com> writes:
> 
> 
>>Pascal Bourguignon wrote:
>>
>>>Gareth McCaughan <················@pobox.com> writes:
>>>
>>>
>>>>Kenny Tilton wrote:
>>>>
>>>>
>>>>
>>>>>Geoffrey King wrote:
>>>>>
>>>>>
>>>>>>(apply #'+ '( 1 2 3))
>>>>>
>>>>>True enough, but the OP asked about a vector.
>>>>
>>>>No, he asked about a list that he'd put in a variable called VECTOR. :-)
>>>
>>>OPs are twisted.
>>>
>>
>>And because he said VECTOR I /saw/ #! That is so cool.
>>
>>:)
> 
> 
> It's because as a lisper, you don't see the syntax!
> 

heh-heh, true, true, that is probably part of it. But... I stared right 
at the singlequote and saw an octothorpe. I know because I remember 
thinking, "Weird, he knows enough to use octothorpe for a vector, but 
not enough to...".

Well, OK, I /do/ have two twenty-inch flatpanels set up three feet away, 
and I could use glasses...still.

:)

kenny
From: André Thieme
Subject: Re: How to sum a list?
Date: 
Message-ID: <1139562916.944391.301030@f14g2000cwb.googlegroups.com>
Kenny Tilton schrieb:

> Duane Rettig wrote:

> > It's because as a lisper, you don't see the syntax!
>
> heh-heh, true, true, that is probably part of it. But... I stared right
> at the singlequote and saw an octothorpe. I know because I remember
> thinking, "Weird, he knows enough to use octothorpe for a vector, but
> not enough to...".

Kenny, I know exactly how you feel because the same thing happened. I
looked at the octothorpe and wondered why the OP asked how to sum a
list when in fact he wants to sum the elements of a vector. Why did we
see something which wasn't there? Or was it there? Might that be an
error in the Matrix?


André
--
From: Gareth McCaughan
Subject: Re: How to sum a list?
Date: 
Message-ID: <873biqxgl1.fsf@g.mccaughan.ntlworld.com>
Andr� Thieme wrote:

> Kenny, I know exactly how you feel because the same thing happened. I
> looked at the octothorpe and wondered why the OP asked how to sum a
> list when in fact he wants to sum the elements of a vector. Why did we
> see something which wasn't there? Or was it there? Might that be an
> error in the Matrix?

No, in the Vector.

-- 
Gareth McCaughan
.sig under construc
From: Kenny Tilton
Subject: Re: How to sum a list?
Date: 
Message-ID: <_qlHf.4301$Lp2.943@news-wrt-01.rdc-nyc.rr.com>
Andr� Thieme wrote:
> Kenny Tilton schrieb:
> 
> 
>>Duane Rettig wrote:
> 
> 
>>>It's because as a lisper, you don't see the syntax!
>>
>>heh-heh, true, true, that is probably part of it. But... I stared right
>>at the singlequote and saw an octothorpe. I know because I remember
>>thinking, "Weird, he knows enough to use octothorpe for a vector, but
>>not enough to...".
> 
> 
> Kenny, I know exactly how you feel because the same thing happened. I
> looked at the octothorpe and wondered why the OP asked how to sum a
> list when in fact he wants to sum the elements of a vector. Why did we
> see something which wasn't there? 


It has to be this way. There is no way our minds can start from scratch 
analyzing every visual pixel in every time slice. So we wake up, figure 
out where we are, and then do incremental updates, quickly incorporating 
new inputs by leveraging what we know already.

A full pixel-by-pixel breakdown of that smudge just before the literal 
list being bound to "vector"? Nah, it's obviously an octothorpe, move on.

Moral: we are lucky to get through the day. :)

What makes this one especially illuminating is that we stared right at 
it and focused on it and thought about it. But by then we were not 
really parsing it any more because we already knew what it was. It shows 
the extent to which objective reality is unknowable.

Which brings us back to Lisp. Everyone knows it is slow and used only 
for AI. Paul Graham told them otherwise rather famously several years 
ago. Only now are folks starting to figure it out. Still slowly. Even 
Peter Coffee, a big booster now, said Lisp was not suited for 
conventional business programming.

That is news to those of us using it for that. :)

kenny
From: Pascal Bourguignon
Subject: Re: How to sum a list?
Date: 
Message-ID: <87acd3nd3g.fsf@thalassa.informatimago.com>
Geoffrey King <··············@optushome.com.au> writes:
> Darrell Adams wrote:
>> I am very new to LISP.
>> I would like to know how to return the sum, over all the elements,
>> of a variable length list.
>> I know that you can use
>> (+ 1 2 3 ...) to get a sum of a few items.
>> If I have an item of variable length as in
>> (setq vector '(1 2 3 ... n))
>> I would like to have the sum returned.
>> I hope this is clear enough.
>
> (apply #'+ '( 1 2 3))


No. This is a FAQ.  The answer is: (reduce (function +) list)

-- 
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
__Pascal Bourguignon__                     http://www.informatimago.com/
From: Darrell Adams
Subject: Re: How to sum a list?
Date: 
Message-ID: <RcmdnYX7QqnWnnTenZ2dnUVZ_tWdnZ2d@comcast.com>
Yes, that was what I was looking for.  I also found the FAQ and will search
that next time before posting.  

Thanks,
Darrell

 
> No. This is a FAQ.  The answer is: (reduce (function +) list)
> 
From: Timofei Shatrov
Subject: Re: How to sum a list?
Date: 
Message-ID: <43e900b9.50500750@news.readfreenews.net>
On Tue, 07 Feb 2006 21:11:31 +0100, Pascal Bourguignon
<······@informatimago.com> tried to confuse everyone with this message:

>Geoffrey King <··············@optushome.com.au> writes:
>> Darrell Adams wrote:
>>> I am very new to LISP.
>>> I would like to know how to return the sum, over all the elements,
>>> of a variable length list.
>>> I know that you can use
>>> (+ 1 2 3 ...) to get a sum of a few items.
>>> If I have an item of variable length as in
>>> (setq vector '(1 2 3 ... n))
>>> I would like to have the sum returned.
>>> I hope this is clear enough.
>>
>> (apply #'+ '( 1 2 3))
>
>
>No. This is a FAQ.  The answer is: (reduce (function +) list)

Also,
(loop for x in '(1 2 3 4) summing x)


-- 
|WAR HAS NEVER SOLVED ANYTHING|,----- Timofei Shatrov aka Grue---------.
|(except for ending slavery,  ||mail: grue at mail.ru ================ |
|   fascism and communism)    ||============= http://grue3.tripod.com  |
|...and Saddam's dictatorship |`----------------------------------[4*72]