2015-03-31 51 views
1

在通用Lisp中,如何讀取&從/到流寫入符號表達式?例如,我可能要編寫一個匿名函數到文件,然後讀取和funcall它:符號表達式流I/O

;;; sexp-io.lisp 
;;; Try writing a sexp to file and reading it back 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)) 

然而,在可疑的輸出碼結果

#<Anonymous Function #x3020018F950F> 

這不會導致一個錯誤,當我嘗試閱讀它放回:

> 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). 
+0

你究竟想要做什麼? – acelent 2015-03-31 22:49:23

回答

5

你正在做TRT,除了#'從而關list(lambda() (+ 1 1))function對象。只需用一個簡單的引號(讀作quote)替換尖括號(讀作function),它就可以工作。

你可能要做出與參數:readably t更換print with write另一個變化:

(write my-object :stream out :readably t) 

:readably的好處是,如果不能的方式,將保留打印讀一致性寫失敗。

+1

TRT代表什麼? – 2015-04-01 02:37:59