[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: take! 0==1?
William ML Leslie
Re: take! 0==1?
Fri, 12 Jul 2013 17:57:02 +1000
On 12 July 2013 17:14, Jan Nieuwenhuizen <address@hidden> wrote:
> Reading the documentation of take!
> -- Scheme Procedure: take lst i
> -- Scheme Procedure: take! lst i
> Return a list containing the first I elements of LST.
> `take!' may modify the structure of the argument list LST in order
> to produce the result.
> its behaviour surpsises me.
> For list LST, (take! lst 0) leaves LST in the same state
> as (take! lst 1) does. Worse, the return value suggests
> that it worked
Actually, I should clarify a few things, but this discussion probably
belongs on guile-user.
Whenever the documentation says 'may', it really means it. You
absolutely cannot rely on the side-effecting behaviour, because an
implementation that does no mutation whatsoever is a valid
implementation of (take!), according to the documentation. All you're
saying by using (take!) is, if it is more efficient to do so by
altering the list, then please do.
However, what it does is very simple to determine by looking at a box
diagram for the following.
scheme@(guile-user)> (use-modules (srfi srfi-1))
scheme@(guile-user)> (define xs '(a b c))
scheme@(guile-user)> (define ys (cdr xs))
scheme@(guile-user)> (define zs (cdr ys))
scheme@(guile-user)> (take! xs 3) ;; writes a nil at the end of the
list, ie does nothing
$3 = (a b c)
scheme@(guile-user)> (take! xs 2) ;; replaces the reference to the
last pair with nil
$11 = (a b)
$12 = (a b)
$13 = (b)
scheme@(guile-user)> zs ;; the last pair still exists, although it is
not reachable from xs/ys
$14 = (c)
Likely much of this email is, by the nature of copyright, covered
under copyright law. You absolutely may reproduce any part of it in
accordance with the copyright law of the nation you are reading this
in. Any attempt to deny you those rights would be illegal without
prior contractual agreement.