with-input-from-string

Synopsis

(with-input-from-string string thunk)

Parameters

  • string
  • thunk

Description

Thunk must be a procedure of no arguments. with-input-from-string creates a new input port that reads from string, makes that port the current input port, and calls thunk. When thunk returns, with-input-from-string restores the previous current input port and returns the result yielded by thunk.

Note: this procedure is equivalent to:

(with-input-from-port (string->input-port string) thunk)

Side Effects

Return Value

A symbolic expression or the EOF object.

Implementation

(define (with-input-from-string string thunk)
 (let ((oldport (current-input-port))
       (newport (open-input-string string)))
   (dynamic-wind
     (lambda () (set-current-input-port! newport))
     thunk
     (lambda () (set-current-input-port! oldport)))))

Example

> (with-input-from-string "(a b c) (d e f)" read)
(a b c)
>