Reputation: 229
How can i read from file recursively, in Common Lisp. I find a lot of examples the are iterative, but i need recursive method. Currently i'm training something like this:
(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))
Upvotes: 1
Views: 719
Reputation: 3930
First, in CL it is good style not having opening or closing paranthesis only on a line.
Here is an approach to read-char
recursively until 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))))
You can use it like this:
(with-open-file (file-stream "input.txt")
(with-output-to-string (string-stream)
(read-recursive file-stream string-stream)
string-stream))
Upvotes: 3