Common Lisp,递归读取文件

时间:2013-11-18 09:17:00

标签: common-lisp

如何在Common Lisp中递归读取文件。我发现很多例子都是迭代的,但我需要递归方法。 目前我正在训练这样的事情:

(defun read-r()
  (let ((in (open 'input.txt)))
    (read-arrayR in)
  )
)

(defun read-arrayR(in)
    ( 
      (lambda()
        (setq num (char (read in nil)
                  (read in nil))
        )
      )
    )
    (if (null num)
      (
        (lambda()
          (colect num)
          (read-arrayR in)
        ) 
      )
    )
)

(setq arr (read-r))

1 个答案:

答案 0 :(得分:2)

首先,在CL中,只有在一条线上才能打开或关闭paranthesis。

以下是递归read-char直到EOF的方法。

(defun read-recursive (stream-in stream-out)
  (let ((char (read-char stream-in nil)))
    (unless (null char)
      (format stream-out "~c" char)
      (read-recursive stream-in stream-out))))

你可以像这样使用它:

(with-open-file (file-stream "input.txt")
  (with-output-to-string (string-stream)
    (read-recursive file-stream string-stream)
    string-stream))
相关问题