I wrote down this iterative code in LISP using the loop function:
(defun loadfile (filename)
(with-open-file (stream filename)
(loop for line = (read-line stream nil 'eof)
until (eq line 'eof)
collect line)))
)
)
Is there a way to rewrite it without loop, in a recursive way?
Surprise them with a GOTO instead:
(defun loadfile (filename)
(with-open-file (stream filename)
(prog (line lines)
repeat
(setf line (read-line stream nil))
(when line
(push line lines)
(go repeat))
(return (reverse lines)))))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With