[gambit-list] How to save s-expressions
mikael.rcv at gmail.com
Sat Oct 27 20:11:31 EDT 2012
Do all ports GC? (am thinking especially about file and tcp client and
I suppose that the fact that with-*-port closes the port on finalization
cannot be relied on to always work, because what if there's an exception
during the thunk's execution and it's handled in a way that never leaves
control back, or the thread is thread-terminate!:d (could happen a
dynamic-wind finalizer would not be trigged right)?
2012/10/27 Marc Feeley <feeley at iro.umontreal.ca>
> Le 2012-10-27 à 4:31 AM, Richard Prescott <rdprescott at gmail.com> a écrit :
> > Good morning everybody,
> > Forgive my ignorance, I am just learning.
> > In order to embrace the "code is data" philosophy , I want to load
> and save data as s-expressions within files. I figured the load using
> (read (open-input-file "data.scm")).
> > How can I save?
> > Thanks in advance.
> > Richard
> While it does work, your code does not close the input-port and relies on
> the garbage collector to do this, which is a bad idea (because you may run
> out of file descriptors before a garbage collection is triggered). It is
> better to use call-with-input-file which closes the port.
> Here are better save and restore functions:
> ;; File: save-restore.scm
> (define (save-to-file filename obj)
> (lambda (port) (write obj port))))
> (define (restore-from-file filename)
> (lambda (port) (read port))))
> (save-to-file "data.txt" '#(1 2 3))
> (pp (restore-from-file "data.txt"))
> Gambit-list mailing list
> Gambit-list at iro.umontreal.ca
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Gambit-list