I/O & Ports

← Back to Language Guide

Eta’s I/O follows the R7RS port model. The runtime provides the core port operations as builtins; std.io adds convenience functions and scoped redirection helpers.


Builtins

OperationPurpose
display, write, newlineWrite a value (display is unquoted, write is read-able)
write-string, write-charDirect character / string output
read-charRead one character
current-input-portThe active input port
current-output-portThe active output port
current-error-portThe active error port
set-current-{input,output,error}-port!Replace the active port
open-input-file pathOpen a file for reading
open-output-file pathOpen a file for writing
open-input-string sRead from an in-memory string
open-output-stringAccumulate output into a string
get-output-string pSnapshot of the string-port contents
close-port, close-input-port, close-output-portClose a port
port?, input-port?, output-port?Predicates

std.io helpers

(import std.io)

(println "hi")                       ; (display x) (newline)
(eprintln "warn: …")                 ; same, but to current-error-port
(read-line)                          ; reads up to newline; #!eof at EOF

(display->string '(1 2 3))           ; => "(1 2 3)"

Scoped redirection

(with-output-to-port (open-output-file "out.txt")
  (lambda ()
    (println "captured to file")))

(define out (open-output-string))
(with-output-to-port out
  (lambda ()
    (println 1) (println 2)))
(get-output-string out)              ; => "1\n2\n"

Companions: with-input-from-port, with-error-to-port, with-port.


File patterns

(define (read-all path)
  (let ((p (open-input-file path)))
    (let loop ((acc '()))
      (let ((line (read-line p)))
        (cond
          ((eof-object? line) (begin (close-input-port p) (reverse acc)))
          (else               (loop (cons line acc))))))))

Tip

Wrap file operations in dynamic-wind (or a custom with-port) so the handle closes even on exceptions:

(define (with-input-file path proc)
  (let ((p (open-input-file path)))
    (dynamic-wind
      (lambda () #f)
      (lambda () (proc p))
      (lambda () (close-input-port p)))))

Structured input

FormatModule
CSVstd.csv
Datalog factsstd.db
Regex on text streamsstd.regex
(import std.csv)
(define rows (csv:load-file "trades.csv" '(:header #t)))