From: Krishna Myneni on
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.
From: Pascal Costanza on
On 25/07/2010 17:57, 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.
>
> Krishna
>
> --
> Example:
>
> (accum 0 '+ '( 1 2 3 ) )
>
> with the result being 6.

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/
From: Frank Buss on
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, fb(a)frank-buss.de
http://www.frank-buss.de, http://www.it4-systems.de
From: Krishna Myneni on
On Jul 25, 10:58 am, Pascal Costanza <p...(a)p-cos.net> wrote:
> On 25/07/2010 17:57, 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.
>
> > Krishna
>
> > --
> > Example:
>
> > (accum  0  '+ '( 1 2 3 ) )
>
> > with the result being 6.
>
> 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/


Thanks, Pascal and Frank!

Krishna
 | 
Pages: 1
Prev: floating point output?
Next: free will