From: ·······@gmail.com
Subject: example doing undo using closures
Date: 
Message-ID: <1194311072.438511.299130@19g2000hsx.googlegroups.com>
could someone show me a trivial example of how i could use closures to
do undo command. Say i have a variable in the lexical scope that i am
changing its value. i suddenly thought i wanted to have a function
with the closure to take the variable back to its previous state.


thanks

George Tasso

From: Andreas Thiele
Subject: Re: example doing undo using closures
Date: 
Message-ID: <fgohr9$3ak$03$1@news.t-online.com>
<·······@gmail.com> schrieb im Newsbeitrag ·····························@19g2000hsx.googlegroups.com...
> could someone show me a trivial example of how i could use closures to
> do undo command. Say i have a variable in the lexical scope that i am
> changing its value. i suddenly thought i wanted to have a function
> with the closure to take the variable back to its previous state.
>
>
> thanks
>
> George Tasso
>

Hi George,

do you mean something like

CL-USER 26 > (let* ((x 3) (r (let ((y x)) (lambda () (setf x y))))) (incf x) (princ x) (funcall r) (print x))
4
3
3

Andreas
From: ·······@gmail.com
Subject: Re: example doing undo using closures
Date: 
Message-ID: <1194315453.391945.59590@k79g2000hse.googlegroups.com>
On Nov 6, 1:00 pm, "Andreas Thiele" <······@nospam.com> wrote:
> <·······@gmail.com> schrieb im ········································@19g2000hsx.googlegroups.com...
>
> > could someone show me a trivial example of how i could use closures to
> > do undo command. Say i have a variable in the lexical scope that i am
> > changing its value. i suddenly thought i wanted to have a function
> > with the closure to take the variable back to its previous state.
>
> > thanks
>
> > George Tasso
>
> Hi George,
>
> do you mean something like
>
> CL-USER 26 > (let* ((x 3) (r (let ((y x)) (lambda () (setf x y))))) (incf x) (princ x) (funcall r) (print x))
> 4
> 3
> 3
>
> Andreas

Exactly thanks Andreas.
From: Kjetil S. Matheussen
Subject: Re: example doing undo using closures
Date: 
Message-ID: <Pine.LNX.4.64.0711061343100.9310@ttleush>
On Mon, 5 Nov 2007, ·······@gmail.com wrote:

> could someone show me a trivial example of how i could use closures to
> do undo command. Say i have a variable in the lexical scope that i am
> changing its value. i suddenly thought i wanted to have a function
> with the closure to take the variable back to its previous state.
>

Here's a general undo/redo "package" I wrote for a scheme program. 
Translation to common lisp is trivial:


(define undos '())
(define redos '())

(define (add-undo redo undo)
   (push! (list redo undo) undos)
   (set! redos '()))

(define (add-undo-run redo undo)
   (add-undo redo undo)
   (redo))

(define (undo)
   (if (not (null? undos))
       (let ((func (car undos)))
 	(set! undos (cdr undos))
 	(push! func redos)
 	((cadr func)))))


(define (redo)
   (if (not (null? redos))
       (let ((func (car redos)))
 	(set! redos (cdr redos))
 	(push! func undos)
 	((car func)))))


(define (reset-undo!)
   (set! undos '())
   (set! redos '()))


#|
(define a 9)
(display a)
(add-undo-run (lambda ()
 		(set! a -5))
 	      (let ((org-a a))
 		(lambda ()
 		  (set! a org-a))))

(display a)
(undo)
(display a)
(redo)
(display a)
|#