ITPub博客

首页 > Linux操作系统 > Linux操作系统 > lisp 习题 用列表元素标识文件一行。

lisp 习题 用列表元素标识文件一行。

原创 Linux操作系统 作者:babyyellow 时间:2012-08-17 14:31:37 0 删除 编辑
定义一个函数,接受一个文件名并返回一个由字串组成的列表,来表示文件里的每一行


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博客 ” ,链接:https://blog.itpub.net/133735/viewspace-741265/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
oracle MySQL Postgresql 专职数据库dba。 系统架构师。 mysql 官方认知dba 。 15年专职dba 经验。 潜意识沟通实证者 心理咨询师。 禅修布道者, 禅修指导员。 禅定实证者。

注册时间:2010-12-02

  • 博文量
    367
  • 访问量
    1908852