0

Is it possible to test procedures that use the read operation?

ex.

(define (foo prompt)
  (display prompt)
  (read))

I tried to use write but read seems to create a block so that the write is only run after I enter something manually

Alter
  • 3,332
  • 4
  • 31
  • 56
  • 1
    You could define your own procedure `(read)` to return whatever value you want. e.g. `(define (read) "Hello!")`. – Flux Jan 20 '20 at 03:05

1 Answers1

0

When you call the internal read in your code, that call will read not from the standard input but from your file itself, because the reader that converts the input file into a list of s-exps will use the same read function. So, yes, you can use it if you redefine it, otherwise the result is harder to anticipate. Here is an example of read.scm:

(define (foo m)
  (display m)
  (read))

(display (foo "input:"))

200

(newline)

Example of use:

% mit-scheme --silent < read.scm
input:200

The value 200 will be read by the function call, it will not be returned in the final list that represents the code.

To define your own read-keyboard functionality you can take as model this.

alinsoar
  • 15,386
  • 4
  • 57
  • 74
  • Just a note: I'm marking this as answered because it works with what information I gave in the question, but it doesn't work in my specific set-up (Dr Racket - R5RS) – Alter Jan 22 '20 at 17:09
  • @Alter Racket is multi-language. If you set it on r5rs it should work. – alinsoar Jan 22 '20 at 17:31
  • perhaps I'm confused, but it seems the `read` with racket and r5rs does seem to use standard input (ie. it gives me a text input box) – Alter Jan 22 '20 at 17:36
  • I cannot help because I used racket only for limited purposes, mainly focusing on other aspects of r6rs, not on i/o interface. – alinsoar Jan 22 '20 at 17:40
  • @Alter Which OS are you using? Are you using Racket r5rs through DrRacket or through the command line? – Flux Jan 29 '20 at 09:03