Common lisp 公共Lisp,从文件中递归读取
如何使用公共Lisp递归读取文件。我发现很多例子都是迭代的,但我需要递归的方法。 目前我正在进行如下培训:Common lisp 公共Lisp,从文件中递归读取,common-lisp,Common Lisp,如何使用公共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))
(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))
(with-open-file (file-stream "input.txt")
(with-output-to-string (string-stream)
(read-recursive file-stream string-stream)
string-stream))
首先,在CL中,它是一种好的风格,不只是在一行上有开始或结束的偏执 下面是一种递归读取字符直到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))))
您可以这样使用它:
(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))
(with-open-file (file-stream "input.txt")
(with-output-to-string (string-stream)
(read-recursive file-stream string-stream)
string-stream))