Mark McWiggins
Mark McWiggins

Reputation: 651

Need help getting CLisp to read standard input into a list

I'm working on converting some existing Python code to CLisp just as an exercise ...

The program reads a list of numbers and creates mean, min, max and standard deviation from the list. I have the file-based function working:

(defun get-file (filename)
   (with-open-file (stream filename)
     (loop for line = (read-line stream nil)
      while line
      collect (parse-float line))))

This works when I call it as

(get-file "/tmp/my.filename")

... but I want the program to read standard input and I've tried various things with no luck.

Any advice?

Upvotes: 3

Views: 226

Answers (2)

acelent
acelent

Reputation: 8135

Just separate concerns:

(defun get-stream (stream)
  (loop for line = (read-line stream nil)
        while line
        collect (parse-float line)))

(defun get-file (filename)
  (with-open-file (stream filename)
    (get-stream stream)))

Then you can use get-file like you already do, and (get-stream *standard-input*).

Upvotes: 6

jlahd
jlahd

Reputation: 6303

The variable *standard-input* is bound to standard input:

(defun get-from-standard-input ()
   (loop for line = (read-line *standard-input* nil)
         while line
         collect (parse-float line)))

Upvotes: 2

Related Questions