Re: Saving results

From: Pascal Bourguignon (spam_at_thalassa.informatimago.com)
Date: 06/24/04


Date: 24 Jun 2004 22:03:20 +0200


"Bruce J. Weimer" <bjweimer@charter.net> writes:

> If in my program somewhere "foo" gets set to some result the program's
> calculated - say 47, then what's the easiest way to have the program save
> "foo" so that when I run the program again later "foo" starts off set as 47
> (assume the number changes every time the program runs, but then I want to
> keep the new number for the next run)? Is there a simple "save to disk" /
> "initialize from disk" methodology?
>
> Sorry if this is too obvious a question - I'm still very much a newbie.

For an example, see this "pesistent" serial counter:

(DEFUN TODAY ()
  "
RETURN: A string containing the date of today formated as YYYYMMDD
"
  (MULTIPLE-VALUE-BIND (SEC MIN HOU DAY MON YEA) (GET-DECODED-TIME)
    (FORMAT NIL "~4,'0D~2,'0D~2,'0D" YEA MON DAY)));;TODAY

(DEFUN NEXT-SERIAL (FILE)
  "
RETURN: A string containing a serial number formated as YYYYMMDDSS.
POST: The serial number is incremented and the file is updated.
"
  (WITH-OPEN-FILE (SDF FILE
                       :DIRECTION :IO
                       :IF-EXISTS :APPEND
                       :IF-DOES-NOT-EXIST :CREATE)
    (FILE-POSITION SDF 0)
    (LET* ((TODAY (TODAY))
           (DATE (IF SDF (READ SDF NIL TODAY) TODAY))
           (SERIAL (IF SDF (READ SDF NIL 0) 0)))
      (IF (STRING-EQUAL TODAY DATE)
        (INCF SERIAL)
        (SETQ DATE TODAY SERIAL 0))
      (FILE-POSITION SDF 0)
      (FORMAT SDF "~S ~D~2%" DATE SERIAL)
      (FORMAT NIL "~A~2,'0D" DATE SERIAL))));;NEXT-SERIAL

If you need to keep the results of several functions, check
"memoizing" and combine the two techniques...

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
There is no worse tyranny than to force a man to pay for what he does not
want merely because you think it would be good for him. -- Robert Heinlein

Loading