Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

accumulate function in Lisp

105 views
Skip to first unread message

Krishna Myneni

unread,
Jul 25, 2010, 11:57:41 AM7/25/10
to
Apologies in advance if this is a naive question, since I'm new to
Lisp (my background is Forth).

Is there an intrinsic function in Common Lisp which will iterate over
each element of the list and apply a binary function between an
integer and the list element. The return value should be an integer.
I'm familiar with "mapcar", "every", and "some", but I can't see how
to adapt them to this problem. I realize one can write such a list
iterator, but my question is whether or not such a function already
exists, or, if not, whether there is a commonly accepted name for such
an iterator. Thanks.

Krishna

--
Example:

(accum 0 '+ '( 1 2 3 ) )

with the result being 6.

Pascal Costanza

unread,
Jul 25, 2010, 11:58:44 AM7/25/10
to

It's called 'reduce.


Pascal

--
My website: http://p-cos.net
Common Lisp Document Repository: http://cdr.eurolisp.org
Closer to MOP & ContextL: http://common-lisp.net/project/closer/

Frank Buss

unread,
Jul 25, 2010, 12:02:00 PM7/25/10
to
Krishna Myneni wrote:

> Apologies in advance if this is a naive question, since I'm new to
> Lisp (my background is Forth).
>
> Is there an intrinsic function in Common Lisp which will iterate over
> each element of the list and apply a binary function between an
> integer and the list element. The return value should be an integer.
> I'm familiar with "mapcar", "every", and "some", but I can't see how
> to adapt them to this problem. I realize one can write such a list
> iterator, but my question is whether or not such a function already
> exists, or, if not, whether there is a commonly accepted name for such
> an iterator. Thanks.

Yes, reduce:

CL-USER 1 > (reduce #'+ '(1 2 3) :initial-value 10)
16

CL-USER 2 >

--
Frank Buss, f...@frank-buss.de
http://www.frank-buss.de, http://www.it4-systems.de

Krishna Myneni

unread,
Jul 25, 2010, 12:12:17 PM7/25/10
to


Thanks, Pascal and Frank!

Krishna

WJ

unread,
Feb 10, 2011, 2:15:45 PM2/10/11
to
Frank Buss wrote:

> Krishna Myneni wrote:
>
> > Apologies in advance if this is a naive question, since I'm new to
> > Lisp (my background is Forth).
> >
> > Is there an intrinsic function in Common Lisp which will iterate
> > over each element of the list and apply a binary function between an
> > integer and the list element. The return value should be an integer.
> > I'm familiar with "mapcar", "every", and "some", but I can't see how
> > to adapt them to this problem. I realize one can write such a list
> > iterator, but my question is whether or not such a function already
> > exists, or, if not, whether there is a commonly accepted name for
> > such an iterator. Thanks.
>
> Yes, reduce:
>
> CL-USER 1 > (reduce #'+ '(1 2 3) :initial-value 10)
> 16
>
> CL-USER 2 >

Clojure:

user=> (reduce + 10 [1 2 3])
16


MatzLisp (Ruby):

[1,2,3].reduce(10){|a,b| a+b}
==>16

Marco Antoniotti

unread,
Feb 10, 2011, 2:53:13 PM2/10/11
to

CL > (reduce '+ #(1 2 3) :initial-value 10 :from-end t)
16

Cheers
--
Marco

Pascal J. Bourguignon

unread,
Feb 10, 2011, 3:10:18 PM2/10/11
to
Marco Antoniotti <mar...@gmail.com> writes:

> CL > (reduce '+ #(1 2 3) :initial-value 10 :from-end t)
> 16

For +, there's no point in using :from-end t.

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

will be faster.

--
__Pascal Bourguignon__ http://www.informatimago.com/
A bad day in () is better than a good day in {}.

WJ

unread,
Mar 6, 2011, 8:24:02 AM3/6/11
to
Frank Buss wrote:

> Krishna Myneni wrote:
>
> > Apologies in advance if this is a naive question, since I'm new to
> > Lisp (my background is Forth).
> >
> > Is there an intrinsic function in Common Lisp which will iterate over
> > each element of the list and apply a binary function between an
> > integer and the list element. The return value should be an integer.
> > I'm familiar with "mapcar", "every", and "some", but I can't see how
> > to adapt them to this problem. I realize one can write such a list
> > iterator, but my question is whether or not such a function already
> > exists, or, if not, whether there is a commonly accepted name for such
> > an iterator. Thanks.
>
> Yes, reduce:
>
> CL-USER 1 > (reduce #'+ '(1 2 3) :initial-value 10)
> 16
>
> CL-USER 2 >

Scheme:

guile> (fold + 10 '(1 2 3))
16

Rainer Joswig

unread,
Mar 6, 2011, 9:14:41 AM3/6/11
to

CL-USER 28 > (reduce '+ (cons 10 '(1 2 3)))
16

Pascal J. Bourguignon

unread,
Mar 6, 2011, 9:36:12 AM3/6/11
to
Rainer Joswig <jos...@lisp.de> writes:

(f '+ 10 '(1 2 3))

Even shorter than guile...

with: (defun f (f i s) (reduce f s :initial-value i))

Notice that contrarily to scheme, or Rainer's solution, f works on
vectors too:

(f '+ 10 (vector 1 2 3))

0 new messages