From: Bruce Feist
Subject: Novice C Question
Date: 
Message-ID: <731784120.AA00000@blkcat.UUCP>
* Original Area: LISP
* Original To  : All (1:109/615)

I'm trying to write a series of Common LISP functions to implement a stopwatch. 
A stopwatch will be a dotted pair with a car equal to the time that the watch
was last started, or NIL if it's currently paused, and a cdr equal to the total
amount of time accumulated by the stopwatch so far (which will be updated
whenever the stopwatch is stopped).  An initialized stopwatch is returned by the
function 'stopwatch', which is defined as:

(defun stopwatch () '(NIL . 0))

The function to turn the stopwatch on is defined as:

(defun stopwatch-start (watch)
    (unless (car watch)   ; if the stopwatch is currently turned off...
        (setf (caroldwatch) (get-internal-run-time)) )
    (cdr oldwatch) )

This isn't working properly.  (stopwatch) appears to always return the same
stopwatch; if a watch's state has been altered by a call to (stopwatch-start),
all future stopwatches created reflect the same changes.

This must be happenning because (stopwatch) is always returning a pointer to the
same structure, and (stopwatch-start) is modifying that structure.  So, the
question is: how do I get (stopwatch) to build a new stopwatch every time it's
called?

Thanks.

From: david kuznick
Subject: Re: Novice C Question
Date: 
Message-ID: <1993Mar10.142320.5811@titan.ksc.nasa.gov>
In article <·················@blkcat.UUCP>, ···········@f615.n109.z1.fidonet.org (Bruce Feist) writes:
|>Relay-Version: VMS News - V6.0-3 14/03/90 VAX/VMS V5.5; site titan.ksc.nasa.gov
|>Path: titan.ksc.nasa.gov!ames!sun-barr!cs.utexas.edu!uunet!blkcat!Uucp
|>Newsgroups: comp.lang.lisp
|>Subject: Novice C Question
|>Message-ID: <·················@blkcat.UUCP>
|>From: ···········@f615.n109.z1.fidonet.org (Bruce Feist)
|>Date: Wed, 10 Mar 1993 07:11:30 -0500
|>Sender: ····@blkcat.UUCP
|>Lines: 30
|>
|>* Original Area: LISP
|>* Original To  : All (1:109/615)
|>
|>I'm trying to write a series of Common LISP functions to implement a stopwatch. 
|>A stopwatch will be a dotted pair with a car equal to the time that the watch
|>was last started, or NIL if it's currently paused, and a cdr equal to the total
|>amount of time accumulated by the stopwatch so far (which will be updated
|>whenever the stopwatch is stopped).  An initialized stopwatch is returned by the
|>function 'stopwatch', which is defined as:
|>
|>(defun stopwatch () '(NIL . 0))
|>
|>The function to turn the stopwatch on is defined as:
|>
|>(defun stopwatch-start (watch)
|>    (unless (car watch)   ; if the stopwatch is currently turned off...
|>        (setf (caroldwatch) (get-internal-run-time)) )
|>    (cdr oldwatch) )
|>
|>This isn't working properly.  (stopwatch) appears to always return the same
|>stopwatch; if a watch's state has been altered by a call to (stopwatch-start),
|>all future stopwatches created reflect the same changes.
|>
|>This must be happenning because (stopwatch) is always returning a pointer to the
|>same structure, and (stopwatch-start) is modifying that structure.  So, the
|>question is: how do I get (stopwatch) to build a new stopwatch every time it's
|>called?
|>
|>Thanks.
|>
|>

When you quote a cons as in '(NIL . 0) you are creating a constant, so
subsequent calls to stopwatch will in fact return the same list.  In fact it
is now considered an error in Common Lispto modify a constant so 
(setf (car stopwatch) ...)
is not allowed (in fact in Lucid disksaves and VAX Lisp small-systems, you
will land in the debugger trying to do this).

So simply redefine stopwatch to
(defun stopwatch () (cons nil 0))

This will return a fresh cons each time stopwatch is called.  Now you can 
setf car's to your heart's content.  :-)

--
**
David Kuznick
·······@meglos.mdcorp.ksc.nasa.gov
MUTLEY! Do something!  - D.D.
From: Len Charest
Subject: Re: Novice C Question
Date: 
Message-ID: <1993Mar10.194436.12310@jpl-devvax.jpl.nasa.gov>
In article <·················@blkcat.UUCP>, ···········@f615.n109.z1.fidonet.org (Bruce Feist) writes:

|> (defun stopwatch () '(NIL . 0))

|> This isn't working properly.  (stopwatch) appears to always return the same
|> stopwatch

Right, because '(NIL. 0) is a constant list. Try:

(defun stopwatch ()
  (cons nil 0))
..................................................
                                  Len Charest, Jr.
                 JPL Artificial Intelligence Group
                          ·······@aig.jpl.nasa.gov