Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You need to <code>quote</code> the structure prior to obtaining the string representation: </p> <pre><code>(list 'quote foo) </code></pre> <p>where <code>foo</code> is the structure.</p> <p>Three additional remarks:</p> <ol> <li><p>traversing the code to quote all lists / seqs would not do at all, since the top-level <code>(defn ...)</code> form would also get quoted;</p></li> <li><p>lists are not the only potentially problematic type -- symbols are another one (<code>+</code> vs. <code>#&lt;core$_PLUS_ clojure.core$_PLUS_@451ef443&gt;</code>);</p></li> <li><p>rather than using <code>(str foo)</code> (even with <code>foo</code> already quoted), you'll probably want to print out the quoted <code>foo</code> -- or rather the entire code block with the quoted <code>foo</code> inside -- using <code>pr</code> / <code>prn</code>.</p></li> </ol> <p>The last point warrants a short discussion. <code>pr</code> explicitly promises to produce a readable representation if <code>*print-readably*</code> is <code>true</code>, whereas <code>str</code> only produces such a representation for Clojure's compound data structures "by accident" (of the implementation) <em>and still only if <code>*print-readably*</code> is <code>true</code></em>:</p> <pre><code>(str ["asdf"]) ; =&gt; "[\"asdf\"]" (binding [*print-readably* false] (str ["asdf"])) ; =&gt; "[asdf]" </code></pre> <p>The above behaviour is due to <code>clojure.lang.RT/printString</code>'s (that's the method Clojure's data structures ultimately delegate their <code>toString</code> needs to) use of <code>clojure.lang.RT/print</code>, which in turn chooses output format depending on the value of <code>*print-readably*</code>.</p> <p>Even with <code>*print-readably*</code> bound to <code>true</code>, <code>str</code> may produce output inappropriate for <code>clojure.lang.Reader</code>'s consumption: e.g. <code>(str "asdf")</code> is just <code>"asdf"</code>, while the readable representation is <code>"\"asdf\""</code>. Use <code>(with-out-str (pr foo))</code> to obtain a string object containing the representation of <code>foo</code>, guaranteed readable if <code>*print-readably*</code> is <code>true</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload