mit-scheme-devel
[Top][All Lists]
Advanced

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

Re: [MIT-Scheme-devel] Min/Max Error when Trying to Delv


From: Taylor R Campbell
Subject: Re: [MIT-Scheme-devel] Min/Max Error when Trying to Delv
Date: Wed, 9 Dec 2009 13:36:03 -0500
User-agent: IMAIL/1.21; Edwin/3.116; MIT-Scheme/7.7.90.+

   Date: Sat, 5 Dec 2009 11:59:32 -0800 (PST)
   From: lcedlightning <address@hidden>

   (define pairmin
           (lambda(x)
                   (delv! (max (car x) (list-ref x 1)) x)))

   and it works just right. It examines the first two elements of a list and
   destroys the larger one. A function call with a variable (define x(list 1 2
   3 4 5)) as the argument to this function would return (1 3 4 5).

If you pass a list to DELV!, you must not use that list any more;
instead, DELV! returns a list with the same elements as the input list
originally had, except for the specified element.  Thus, you must use
DELV! exactly as you use DELV -- the only difference is that using
DELV! also means that you promise never to use the input list again,
whereas with DELV you may continue to use it to your heart's content.
Here are two valid definitions of DELV!:

(define (delv! element list)
  (delv element list))

(define (delv! element list)
  (let ((list* (delv element list)))
    (if (pair? list)
        (begin (set-car! list 'YOU)
               (set-cdr! list 'LOSE)))
    list*))

The definition in MIT Scheme, though, reuses pairs from the input list
when constructing the output list, in order to reduce the number of
pairs created.  Thus, it may appear to do what you want, for certain
inputs.  But it cannot always do that, no matter how you try to define
it -- consider the following:

(let ((x (list 5)))
  (pp (pair? x))
  (delv! 5 x)
  (pp (pair? x)))

In Scheme, the two calls to (PAIR? X) will always return the same
value -- no procedure has access to the lexical binding of X but the
one implicit in the LET, which does not assign X, and thus no
procedure can change what object X refers to, so that it is always a
pair.  If DELV! were to delete 5 from that list, though, so that X's
value became an empty list, then X would cease to be a pair, violating
the semantics of Scheme.

   From here, you can see that x is at stack position 11. When pairmin
   is called, the memory at stack position 11 is modified. When
   pairmax is called, stack position 12 is modified (a list copy is
   pushed onto the stack).

I'm not sure where you get the idea that there is a stack involved,
but the numbers you see such as 123 in

;Value 123: (frobblethorpe ziptwiddle)

are simply numbers for the values there printed, which you can refer
back to using the lexical syntax `#@'.  For example, after the above,
if I evaluated

(pp 'address@hidden)

then Scheme would pretty-print the list (frobblethorpe ziptwiddle).




reply via email to

[Prev in Thread] Current Thread [Next in Thread]