[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Bug & co
On Wed, Dec 12, 2001 at 03:12:01PM +0100, Jerzy Karczmarczuk wrote:
> How, for goodness' sake, l can guess what happened to the data it points to?
> It continues to point to the *same cell*.
>
> This is - sorry to appear harsh - an *elementary* fact belonging to what we
> call "call by value" (here: dereferenced argument).
The semantics of reverse! are not _completely_ obvious, especially not
to someone who's used to, say, arrays. For example, the original poster
_might_ have expected reverse! to simple rearrange the _elements_ in the
list without modifying the structure, like this:
(define (reverse! l)
(define (aux curr)
(if (null? curr)
l
(let ((my (aux (cdr curr))))
(and my
(not (eq? my curr))
(not (eq? my (cdr curr)))
(let ((tmp (car my)))
(set-car! my (car curr))
(set-car! curr tmp)
(cdr my))))))
(aux l)
l)
Of course this is inefficient and generally a bad idea, but still, it's
not "elementary", and certainly not obvious from the description "list
is destructively reversed".
Lauri Alanko
la@iki.fi
- Follow-Ups:
- Re: Bug & co
- From: Jerzy Karczmarczuk <karczma@info.unicaen.fr>
- References:
- Bug & co
- From: Manuel Giraud <giraud@tif.inria.fr>
- Re: Bug & co
- From: Yannis BRES <Yannis.BRES@cma.inria.fr>
- Re: Bug & co
- From: Jerzy Karczmarczuk <karczma@info.unicaen.fr>