another newbie quandary
- From: Dave Slayton <lwoj.sjuwklim@xxxxxxxxx>
- Date: Mon, 11 Jun 2012 17:54:34 -0700 (PDT)
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
.
- Follow-Ups:
- Re: another newbie quandary
- From: drlat
- Re: another newbie quandary
- Prev by Date: Re: lisp newbie here
- Next by Date: Re: another newbie quandary
- Previous by thread: Re: lisp newbie here
- Next by thread: Re: another newbie quandary
- Index(es):
Relevant Pages
|