ENOSIG Discussie (threads)


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: scheme vraagje: is (accumulate cons nil sequence) zinvol? (was: Re: talks at meetings)


On Thu, Sep 09, 2004 at 08:48:08AM +0200, Lionel Elie Mamane wrote:
> On Thu, Sep 09, 2004 at 08:23:01AM +0200, Joost van Baal wrote:

>> (define (accumulate op initial sequence)
>>   (if (null? sequence)
>>       initial
>>       (op (car sequence)
>>           (accumulate op initial (cdr sequence)))))

>> (accumulate cons nil (list 1 2 3 4 5))
>>  geeft
>> (1 2 3 4 5)

> As your example shows, (accumulate cons nil) is the identity on
> lists [1], i.e. the same as (lambda (x) x) [2].

Argh, I'm so used to MLy semantics that I often "forget" the
following: The difference between the two, obviously, is that the
firs creates a *copy* of the list, while the second returns the list
itself. This matters, not only performance-wise, but also for
semantics in Scheme because all objects are mutable: Someone might do
a set-car! on this list later!

Compare:
> (define l (list 1 2 3 4 5))
> (define l0 (accumulate cons '() l))
> (define l1 ((lambda (x) x) l))
> l
'(1 2 3 4 5)
> l0
'(1 2 3 4 5)
> l1
'(1 2 3 4 5)
> (set-car! l 18)
> l
'(18 2 3 4 5)
> l0
'(1 2 3 4 5)
> l1
'(18 2 3 4 5)

<<inline: signature.asc>>


Gerelateerd:


[ Date Index] [ Thread Index]