lisp - symbolic expression stream I/O -
in common lisp, how can 1 read & write symbolic expressions from/to streams? example, might want write anonymous function file , read , funcall it:
;;; sexp-io.lisp ;;; try writing sexp file , reading in (with-open-file (file "~/documents/lisp/concurrency/sexp.lisp" :direction :output :if-exists :supersede) (print #'(lambda () (+ 1 1)) file)) (with-open-file (file "~/documents/lisp/concurrency/sexp.lisp" :direction :input) (read file)) however, code results in dubious output
#<anonymous function #x3020018f950f> which result in error when try reading in:
> error: reader error on #<basic-file-character-input-stream ("/users/frank/documents/lisp/concurrency/sexp.lisp"/7 utf-8) #x3020018f559d>, near position 3, within " > #<anonymous ": > "#<" encountered. > while executing: ccl::signal-reader-error, in process listener(4).
you doing trt, except #' turns list (lambda () (+ 1 1)) function object. replace sharp-quote (which read function) simple quote (which read quote) , should work.
another change might want make replacing print write argument :readably t:
(write my-object :stream out :readably t) the benefit of :readably fails if cannot write in way preserve print-read consistency.
Comments
Post a Comment