phlie
phlie

Reputation: 1385

Writing followed by reading of *standard-input* Common Lisp

This is a very simple question. I am trying to solve the HackerRank questions but don't fully understand how I can write to *standard-input* in order to run the code on my computer.

It asks to sum an array given the length of the array (N) followed by the array itself all on *standard-input*.

Hackerranks uses *standard-input* to give values and it would be easiest if I could store values in the input and then read them.

My question is how can I write to *standard-input*? This will make it a lot easier to work on my computer instead of in the cloud.

Upvotes: 2

Views: 406

Answers (2)

Rainer Joswig
Rainer Joswig

Reputation: 139251

Pure Common Lisp does not provide streams, where you can easily write to, buffer the output, and read it back. Pure Common Lisp also does not provide extensible streams. But there is an extension called gray streams (proposed by David N. Gray as ANSI CL issue STREAM-DEFINITION-BY-USER), which allows to implement pipe streams with a fifo buffer.

Example for a pipe: cl-plumbing

Upvotes: 2

jkiiski
jkiiski

Reputation: 8411

Use with-input-from-string.

(with-input-from-string (s "4 3 2")
  (let ((a (read s))
        (b (read s))
        (c (read s)))
    (format t "~a, ~a, ~a~%" a b c))) 

You could also just read from a file, but reading from a string is much easier for making different test cases.

Upvotes: 5

Related Questions