Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to store computer-generated data?

101 views
Skip to first unread message

Florian Dietz

unread,
Oct 14, 2012, 6:37:36 AM10/14/12
to
How can I store a computer-generated function or data in a file without having to write source code and without loading dependencies?

For instance, imagine the program has generated the equivalent to the following two lines of source code:
(defconstant a "meh")
(defconstant b (lambda () a))

I want to store b in a file without also storing a. When I reload b from the file it should take whatever a is currently defined, not the "meh" above, just as if I had entered and interpreted the source code again.

budden

unread,
Oct 14, 2012, 7:06:05 AM10/14/12
to
Hi! Try to put into a separate file and use compile-file for writing and load for reading.
> (defconstant b (lambda () a))
It might not work for constants though. Consider replacing defconstant with
defparameter or defvar.

Florian Dietz

unread,
Oct 14, 2012, 7:55:56 AM10/14/12
to
I do not have those two lines as source code. I know how to write and load files with source code, but I can't do that here.
Imagine that the two lines have just been executed and that their source code is unknown. How could I do it then? If it helps, it could also work with defparameter instead of defconstant.

budden

unread,
Oct 14, 2012, 9:36:11 AM10/14/12
to
> I do not have those two lines as source code. I know how to write and load files with source code, but I can't do that here. Imagine that the two lines have just been executed and that their source code is unknown. How could I do it then? If it helps, it could also work with defparameter instead of defconstant.
What do you have then? If you have function object, you can wrap it into another, named function, and dump it into a compiled file (I guess).

Kaz Kylheku

unread,
Oct 14, 2012, 1:04:58 PM10/14/12
to
But, by definition, loading the file is exactly as if you had entered and
interpreted the source again. Because entering and interpreting the code again
is what load does, by definition.

If you want to define an a which is not affected by a subsequent defintion,
use defvar:

(defvar a "meh") ;; if a is already defined, nothing happens

this differs from defparameter:

(defparameter a "meh") ;; if a is already defined, assign "meh" to it

See, some hackers had the same problem decades before you and already
solved it.

Constants are not the appropriate tool for this, however, and do not support
this usage. If there is the possibility that a constant a already exists, but
its value is not "meh", then a is not really behaving as a constant! A
constant has the same definition everywhere.

If you suspect that some quantity may change when pieces of the program
are recompiled and reloaded, you cannot make it a constant.

constants are allowed to be inlined into compiled code, so that if you redefine
a constant, existing parts of the program (compiled, optimized code) will
continue to use the old value.

Tobias Höppner

unread,
Oct 15, 2012, 9:56:15 AM10/15/12
to
if you use slime ( i suppose you do) - you can use dribble

> (dribble "outfile.txt")
> a
> b
>(dribble)
*closing message of file*

a longer example is here:
http://stackoverflow.com/questions/7424307/can-i-save-source-files-in-clisp

Pascal J. Bourguignon

unread,
Oct 19, 2012, 8:24:44 AM10/19/12
to
here B is bound to a closure. In general, implementations cannot write
closure to files. The fact that A is bound to a constant does not imply
that the lambda expression is not a closure.

if you dont know the defining form of the free variables in your closure,
you can still generate a source that is writeable if those values are
printable readably, collecting the free variables, and binding them with
let to their values:

(print (list 'let (list (list 'a a)) '(defparameter *b* (lambda () a))))
srcfile)
; sorry the ipad virtual keyboard does not have backquote

--
__Pascal J. Bourguignon__
0 new messages