From: Bruce J. Weimer
Subject: Saving results
Date: 
Message-ID: <10dlqbed86pde90@corp.supernews.com>
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.

Bruce.

From: Erann Gat
Subject: Re: Saving results
Date: 
Message-ID: <gat-A5C285.10011024062004@nntp1.jpl.nasa.gov>
In article <···············@corp.supernews.com>,
 "Bruce J. Weimer" <········@charter.net> wrote:

> 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.

Actually, it's a very good question, and it uncovers a deep subtlety of 
Lisp that makes it unique from other programming languages.

The subtlety is:

> when I run the program again later

In Lisp, there is generally no such thing as "the program".  There is a 
computational environment, within which there might exist a program, or 
many programs, or little fragmentary pieces of programs.  This is one of 
the great features of Lisp because it allows to run (parts of) a program 
before it's finished.

So there are many things you can do:

1.  You can save "the environment", that is, you can save all your 
programs, pieces of program, results, partial results, etc. all at once.  
This is called "dumping an image".  It's not a standard part of Lisp, 
but all implementations have this feature.  But it's invoked differently 
in every implementation so you'll have to RTFM.

2.  You can save selected pieces of the environment to achieve different 
effects.  For example, you can save just "results" in the usual way by, 
for example, writing the results to a file.  You can also save programs 
or program fragments or (other kinds of) data objects to a file.  This 
is generally done by writing Lisp code fragments to a file.  Read up on 
and play around with "make-load-form".

There are other possibilities as well, but I'll leave it at that for now.

E.
From: Kenny Tilton
Subject: Re: Saving results
Date: 
Message-ID: <%FCCc.233126$WA4.79151@twister.nyc.rr.com>
Bruce J. Weimer wrote:

> 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?

maybe you want "with-open-file"? works for input and/or output. for 
output you have write, print, and format. for input you have "read".

> 
> Sorry if this is too obvious a question - I'm still very much a newbie.

c.l.l. is full of newby huggers. almost all of them know more about Lisp 
I/O than I, btw. I do gui stuff.

kt

-- 
Home? http://tilton-technology.com
Cells? http://www.common-lisp.net/project/cells/
Cello? http://www.common-lisp.net/project/cello/
Why Lisp? http://alu.cliki.net/RtL%20Highlight%20Film
Your Project Here! http://alu.cliki.net/Industry%20Application
From: JP Massar
Subject: Re: Saving results
Date: 
Message-ID: <qd1md0pntdengv5eljcvspd1sjld05fj6i@4ax.com>
On Thu, 24 Jun 2004 07:47:11 -0700, "Bruce J. Weimer"
<········@charter.net> wrote:

>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.
>
>Bruce.
>

You can save the whole image as previously noted, which is
implementation-dependent.

Or you can just save the state you want (foo) and read it back in
when your program starts up again.

If you have a global variable, here is one way to save it and its
value to a file  

(defun write-global-to-file (global file)
  (with-open-file (p file :direction :output :if-exists :supersede)
      (format p "~S~%" `(setq ,global ,(symbol-value global))))

So

(setq *foo* 47)

(write-global-to-file '*foo* "C:/foo.lisp")

then in your next session

(load "C:/foo.lisp")

--------------------------------------

To really do this right you want to be able to write out a set of
state variables to the file, not just one, and you may have to deal
with package
issues if your state variables are in different packages, and/or 
you want to put an (in-package ...) form at the top of the
state-variable file, blah, blah, blah.

But that is the basic idea.
From: Marco Baringer
Subject: Re: Saving results
Date: 
Message-ID: <m2isdh3sfn.fsf@convey.it>
"Bruce J. Weimer" <········@charter.net> writes:

> Sorry if this is too obvious a question - I'm still very much a newbie.

some more details would be very very helpful. however, most lisps
have a save-image function which dumps all the state in the image to
disk, maybe this is what you want? what lisp are you using?

-- 
-Marco
Ring the bells that still can ring.
Forget your perfect offering.
There is a crack in everything.
That's how the light gets in.
     -Leonard Cohen
From: Wade Humeniuk
Subject: Re: Saving results
Date: 
Message-ID: <cXDCc.3980$933.2523@clgrps12>
Bruce J. Weimer wrote:
> 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.


(defun save-to-disk (expression filename)
   (with-open-file (stream filename :direction :output
                           :if-exists :supersede
                           :if-does-not-exist :create)
     (write expression :stream stream)))

(defun recover-from-disk (filename)
   (with-open-file (stream filename :direction :input)
     (read stream)))

(defun test (exp)
   (save-to-disk exp "calc.sav")
   (prin1 (recover-from-disk "calc.sav"))
   (fresh-line)
   (save-to-disk (cons :changed exp) "calc.sav")
   (prin1 (recover-from-disk "calc.sav"))
   (fresh-line))

--  Transcript --

CL-USER 5 > (test '(:test 47))
(:TEST 47)
(:CHANGED :TEST 47)
T

CL-USER 6 > (recover-from-disk "calc200bogus.sav")

Error: The file #P"d:/cygwin/home/wade/calc200bogus.sav" does not exist.
   1 (continue) Try opening "calc200bogus.sav" again.
   2 (abort) Return to level 0.
   3 Return to top loop level 0.

Type :b for backtrace, :c <option number> to proceed,  or :? for other options

CL-USER 7 : 1 >

The only caveat is that the expression written by save-to-disk should be able to
be printed readably.

Wade
From: Pascal Bourguignon
Subject: Re: Saving results
Date: 
Message-ID: <87d63o206v.fsf@thalassa.informatimago.com>
"Bruce J. Weimer" <········@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
From: Bruce J. Weimer
Subject: Re: Saving results
Date: 
Message-ID: <10dodimdc3emu4d@corp.supernews.com>
I want to thank you all.  I've carefully read each post and I've learned
something from each one.  I believe that the "with-open-file" suggestions
and examples are just what I was looking for and should serve my purposes.
This is a great group and thank you all again for your willingness to help!

Bruce.


"Bruce J. Weimer" <········@charter.net> wrote in message
····················@corp.supernews.com...
> 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.
>
> Bruce.
>
>