[plt-scheme] quick way to save a hash table to disk?

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Fri May 2 09:04:56 EDT 2003

At Thu, 1 May 2003 15:05:07 -0400, Sean McCorkle wrote:
>   Folks:  Is there an easy way to save (write) a hash table to disk
>   (ideally in a binary form that could be reloaded quickly?).  

Depends on what's in the hash table.

"Yes", if everything in the hash table can be printed with `write' and
read back in with `read'. Then, in the CVS version of MzScheme (soon to
be released as v204), `(print-hash-table #t)' enables hash-table
printing of the form `#hash((<kay> . <val>) ...)', and `read' can read
this form back in.


Here's a little hack for a more compressed "binary" form:

 (define (marshal-to-string v)
   (let ([s (open-output-string)])
    (write (compile `(quote ,v)) s)
    (get-output-string s)))

 (define (unmarshal-from-string s)
   (parameterize ([read-accept-compiled #t])
     (eval (read (open-input-string s)))))

This hack works when everything in `v' could be a literal in source
code.


More general marshaling and unmarshiling support in MzScheme remains a
high priority.

Matthew



Posted on the users mailing list.