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
> (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
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
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
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...
(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.
<·······@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
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.
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
>>>>> "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
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.
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
(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
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
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
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
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
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
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
"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.
"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")
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
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é
--
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
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
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/
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)
>
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]