liveBook · Manning

Read-string

As explained in Clojure: The Essential Reference

read-string is a Clojure function that parses a string as a Clojure expression and returns its data-structure representation.

read-string parses a string containing a Clojure expression and returns its data-structure representation. Reading happens before macro expansion or evaluation, so the result is data rather than an immediately executed program. section 17.1 section 17.10

Use read-string when input is already held as a string:

  • Pass the string as its input; read-string otherwise follows the options and behavior of read. section 17.1
  • Receive data such as a list representing (+ 1 2), rather than the result 3. section 17.1
  • Supply :eof to control the result when the input contains nothing to read. section 17.1
  • Control reader evaluation and other reader features through the same mechanisms available to read. section 17.1

Parsing a form

(read-string "(+ 1 2)") ;; (+ 1 2) (read-string {:eof "nothing to read"} "") ;; "nothing to read" The returned form can be inspected, transformed, or passed separately to eval when execution is intended. section 17.1 section 17.10

(read-string "(+ 1 2)") ; #1
;; (+ 1 2)

(read-string {:eof "nothing to read"} "") ; #2
;; "nothing to read"

Browse executable examples showing ordinary parsing and customized empty-input behavior. section 17.1

How Clojure: The Essential Reference covers it

How deep it goes
Defines parsing, options, EOF handling, and reader controls, while leaving compilation and execution to eval.
Examples
Uses arithmetic forms, empty input with :eof, macro inspection, and a traffic-light DSL translation layer.
What you can do after
Evaluate/understand parsed forms as data and choose whether to inspect, transform, or evaluate them.
How it connects
Connects reading with eval, macro expansion, tagged literals, reader conditionals, and metaprogramming.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Clojure: The Essential Reference. The section links point to the parts of the book it draws on.