Message from discussion
accumulate function in Lisp
Path: g2news1.google.com!news2.google.com!news.glorb.com!news2.glorb.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: "Pascal J. Bourguignon" <p...@informatimago.com>
Newsgroups: comp.lang.lisp
Subject: Re: accumulate function in Lisp
Date: Sun, 06 Mar 2011 15:36:12 +0100
Organization: Informatimago
Lines: 47
Message-ID: <87oc5os4df.fsf@kuiper.lan.informatimago.com>
References: <be39d3a1-581b-4513-86d3-1f669d4592ba@f6g2000yqa.googlegroups.com>
<153kc9xqm5bc5$.mxkffxpq2v5d.dlg@40tude.net>
<il01ti01sn0@enews2.newsguy.com>
<dc4c71a6-df08-4e5b-b1ec-b47e2f257622@s18g2000vbe.googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
X-Trace: individual.net 5BwgV1zUZLaS2hpE6pNBvAmDWISy8RxnXg3t3F5TzuL0rMMPzI
Cancel-Lock: sha1:N2VjY2ViZmIxMzMzNWE4MDE3NTc4YTY4NmY2MGI3MDVhYjNjMGRhYg== sha1:r9fZbIHHVtf2x9HrRQHDhyQLjYY=
Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwAQMAAABtzGvEAAAABlBMVEUAAAD///+l2Z/dAAAA
oElEQVR4nK3OsRHCMAwF0O8YQufUNIQRGIAja9CxSA55AxZgFO4coMgYrEDDQZWPIlNAjwq9
033pbOBPtbXuB6PKNBn5gZkhGa86Z4x2wE67O+06WxGD/HCOGR0deY3f9Ijwwt7rNGNf6Oac
l/GuZTF1wFGKiYYHKSFAkjIo1b6sCYS1sVmFhhhahKQssRjRT90ITWUk6vvK3RsPGs+M1RuR
mV+hO/VvFAAAAABJRU5ErkJggg==
X-Accept-Language: fr, es, en
X-Disabled: X-No-Archive: no
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.2 (gnu/linux)
Rainer Joswig <jos...@lisp.de> writes:
> On 6 Mrz., 14:24, "WJ" <w_a_x_...@yahoo.com> wrote:
>> 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
>
> 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))
--
__Pascal Bourguignon__ http://www.informatimago.com/
A bad day in () is better than a good day in {}.