schemer5rs

R5RS - How to test procedures that use `read` operation


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


Solution

  • 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.