lisp 習題 用列表元素標識檔案一行。

babyyellow發表於2012-08-17
定義一個函式,接受一個檔名並返回一個由字串組成的列表,來表示檔案裡的每一行


CL-USER> (defun pseudo-cat (file)
  (with-open-file (str file :direction :input)
    (let ((x nil))
    (
     do
     ((line (read-line str nil 'eof)
               (read-line str nil 'eof)))
        ((eql line 'eof))
       (push line x))x)))

STYLE-WARNING: redefining COMMON-LISP-USER::PSEUDO-CAT in DEFUN

PSEUDO-CAT
CL-USER> (pseudo-cat "COPYING")
(" "Public License instead of this License.  But first, please read"
 "the library.  If this is what you want to do, use the GNU Lesser General"
 "may consider it more useful to permit linking proprietary applications with"
 "into proprietary programs.  If your program is a subroutine library, you"
 "  The GNU General Public License does not permit incorporating your program"



轉化為表示式

CL-USER> (defun pseudo-cat (file)
  (with-open-file (str file :direction :input)
    (let ((x nil))
    (
     do
     ((line (read-line str nil 'eof)
               (read-line str nil 'eof)))
        ((eql line 'eof))
       (push (make-array 1 :initial-element line) x))
     (reverse x))))
STYLE-WARNING: redefining COMMON-LISP-USER::PSEUDO-CAT in DEFUN
PSEUDO-CAT
CL-USER> (pseudo-cat "copying")
(#("                    GNU GENERAL PUBLIC LICENSE")
 #("                       Version 3, 29 June 2007") #("")
 #(" Copyright (C) 2007 Free Software Foundation, Inc.  #(" Everyone is permitted to copy and distribute verbatim copies")
 #(" of this license document, but changing it is not allowed.") #("")
 #("                            Preamble") #("")
 #("  The GNU General Public License is a free, copyleft license for")


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/133735/viewspace-741265/,如需轉載,請註明出處,否則將追究法律責任。

相關文章