another newbie quandary



I was working with the code in Graham's book some more, and came to
the really interesting program that digests some text file full of
words, in his case Paradise Lost, and keeps track of what words
"typically" can come after each given word, then then there's a
function to generate pseudo-random text from it. I got a copy of The
Pit and the Pendulum, and used that. Also I wanted to have functions
to save the hash table thus generated to a text file so I could just
read it in the next time. I thought I could create an assoc list from
the entire hash table and output that with a single call to prin1 and
it seemed to be working until I noticed that the generated hash table
had 1608 entries and the one I'd written out to the file and read back
in had only 1605 entries. I thought if a person wrote stuff out with
prin1 and back in with read that all the gritty details would be
handled for them, despite it being one BIG list, so, I thought, one
single Lisp object. Am I doing something wrong? Here's the code for
converting the hash table into an assoc list and back again:

(defun hash-table-to-assoc (tbl)
(let (a)
(maphash #'(lambda (k v) (push (cons k v) a)) tbl)
a))

(defun assoc-to-hash-table (a)
(let ((tb (make-hash-table)))
(dolist (i a tb)
(setf (gethash (car i) tb) (cdr i)))))

(defun save-words-assoc-to-file (filename)
(with-open-file (s filename :direction :output :if-
exists :supersede)
(prin1 (hash-table-to-assoc *words*) s) (terpri s)))

(defun read-ready-made-assoc-file (pathname)
(with-open-file (s pathname :direction :input)
(setf *words* (assoc-to-hash-table (read s))))) ;; *words*
is defparametered early in the rest of the code
.



Relevant Pages

  • Re: another newbie quandary
    ... to save the hash table thus generated to a text file so I could just ... in had only 1605 entries. ... prin1 and back in with read that all the gritty details would be ... (defun hash-table-to-assoc (tbl) ...
    (comp.lang.lisp)
  • Re: Most efficient way of converting a hash table to a 2D array (or other easily serializable object
    ... Since PRIN1 does not preserve the hash contents to the output file (it ... using PRIN1 on the resulting array does ... even for hash tables containing around 25,000 entries, it takes several ...
    (comp.lang.lisp)
  • Re: If not readdir() then what?
    ... All we have to do is cache the open files. ... We keep these in an LRU list and a hash table. ... hash LRU chain, but rather a number of LRU chains. ... Have a rbtree for storing directory entries. ...
    (Linux-Kernel)
  • Re: Comparision of C Sharp and C performance
    ... number of table entries approaches the size of the table. ... list originating at each entry. ... Most of the time when a hash is preferable over a tree [or other ... structure] is when the symbol set is of known dimensions. ...
    (comp.lang.c)
  • Re: Suggestions for double-hashing scheme
    ... chain style and reprobe style are basically a wash. ... will be a smaller chance of encountering deleted entries before it. ... Once you sufficiently optimize a hash table, ... by computing of the hash function). ...
    (comp.programming)