Change the current input port in the racket

How can I change the input port in a racket?

That is, suppose I create a new input port:

(define my-port (open-input-string "this is a test")) 

How can I make (current-input-port) now return my-port ?

+4
source share
2 answers
 (current-input-port my-port) 

Do not do this with racket REPL! This will cause all subsequent REPL inputs to come from this source. (However, it works fine inside DrRacket, even in DrRacket REPL.)

+3
source

To add to Chris's answer; the current input port is what is known as a โ€œparameterโ€, which is approximately approximately a parameter / variable with a dynamic scope. In general, it is cleaner and more conservative to set the current input port only temporarily using the parameterize parameter. Like this:

 (parameterize ([current-input-port my-port]) ... do some stuff ... ) 

Evaluation of this code will cause the input port to be set for your body code and any code that it calls, but will not "flow" into code that is evaluated externally; it also cancels the change on an exclusive or continued exit.

+7
source

Source: https://habr.com/ru/post/1381237/


All Articles