[plt-scheme] mreverse!

From: Robby Findler (robby at eecs.northwestern.edu)
Date: Sat Jan 24 16:00:01 EST 2009

That is a correct version of mreverse!. If all you wanted was the
reversed list bound to lst, you didn't need mutable pairs at all, tho.
Just do this:

#lang scheme
(define lst (list 1 2 3 4))
(set! lst (reverse lst))


On Sat, Jan 24, 2009 at 2:57 PM, praimon <praimon at gmail.com> wrote:
> hello,
>
> (require scheme/mpair)
>
> (define lst (mlist 1 2 3 4)
> (mreverse! lst)
>> {4 3 2 1}
> lst
>> {1}
>
> Unless I'm misunderstanding the point of mreverse!, that
> seems like the wrong answer. Shouldn't lst evaluate to
> {4 3 2 1}?
>
> Here's the code for the function:
>
> (define (mreverse! l)
>  (let loop ([l l][prev null])
>    (cond
>     [(null? l) prev]
>     [else (let ([next (mcdr l)])
>             (set-mcdr! l prev)
>             (loop next l))])))
>
> The original list stops mutating after the first iteration
> of the loop, i.e., after (set-mcdr! l null), which explains
> the above result.
>
> I tried fiddling with this (I assume the solution is very
> simple) without success. I did write a rube goldberg-like
> destructive reverse, but I'm eager to see what an elegant
> solution looks like.
>
> regards,
> praimon
> _________________________________________________
>  For list-related administrative tasks:
>  http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>
>


Posted on the users mailing list.