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/
> 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
Thanks, Pascal and Frank!
Krishna
> 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
CL > (reduce '+ #(1 2 3) :initial-value 10 :from-end t)
16
Cheers
--
Marco
> 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 {}.
> 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
CL-USER 28 > (reduce '+ (cons 10 '(1 2 3)))
16
(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))