# Feedback for glossary entry on formatting/print/write/display

**URL:** <https://racket.discourse.group/t/feedback-for-glossary-entry-on-formatting-print-write-display/1877>\
**Category:** Questions & Answers\
**Tags:** glossary, read, print, write\
**Created:** [April 20, 2023, 10:49am UTC](https://racket.discourse.group/t/feedback-for-glossary-entry-on-formatting-print-write-display/1877 "2023-04-20T10:49:47Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![sschwarzer](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/sschwarzer/32/1940_2.png) [@sschwarzer](https://racket.discourse.group/u/sschwarzer)\
**Post date:** [April 20, 2023, 10:49am UTC](https://racket.discourse.group/t/feedback-for-glossary-entry-on-formatting-print-write-display/1877/1 "2023-04-20T10:49:48Z")

</div>

I'm currently preparing the [Racket Glossary](https://docs.racket-lang.org/racket-glossary/) entry for formatting and showing Racket values. My current notes are [here](https://git.sr.ht/~sschwarzer/racket-glossary/tree/main/item/glossary-notes.md#formatting-format-a-etc). (The title for the entry may change; but I'm not yet sure to what. 😉 .)

I've read most of the content in these documents:

- In the Racket Guide:
  - [8.3&nbsp;Reading and Writing Racket Data](https://docs.racket-lang.org/guide/read-write.html)

- In the Racket Reference:
  - [1.3&nbsp;The Reader](https://docs.racket-lang.org/reference/reader.html)
  - [1.4&nbsp;The Printer](https://docs.racket-lang.org/reference/printing.html)
  - [13.4&nbsp;Reading](https://docs.racket-lang.org/reference/Reading.html)
  - [13.5&nbsp;Writing](https://docs.racket-lang.org/reference/Writing.html)

I have a few questions/remarks on what I read so far. I suppose none of these are essential for writing the glossary entry, but I'm curious, especially regarding the first of the following issues.

**Confusion about `read` and `write`**

[8.3&nbsp;Reading and Writing Racket Data](https://docs.racket-lang.org/guide/read-write.html) says on `write`:

> [write](https://docs.racket-lang.org/reference/Writing.html#%28def._%28%28quote._~23~25kernel%29._write%29%29), which prints a value in such a way that [read](https://docs.racket-lang.org/reference/Reading.html#%28def._%28%28quote._~23~25kernel%29._read%29%29) on the output produces the value back

As a simple example, I noticed that `(write 'foo)` emits `foo`, not `'foo`, as I had expected. I thought the "reader" is the code that parses Racket (`#lang racket`) code in files and in the REPL, but then the output should be `'foo`. If `read` isn't intended for what I expected, what is its role? On the other hand, what is the corresponding infrastructure/function(s) that would translate the input string `"'foo"` to the Racket data `'foo`?

**"Quoting depth" and "quotable"**

[This paragraph](https://docs.racket-lang.org/reference/printing.html#%28tech._quoting._depth%29) has some information on the terms "quoting depth" and "quotable", but honestly that doesn't tell me anything about the actual meaning or purpose of these concepts. 🙂 I thought it might have something to do with pretty-printing and experimented with the `quoting depth` argument of `print` in the REPL, but didn't see any differences in the output, regardless of using `quoting-depth` 0 or 1.

What do "quotable" and "quoting depth" actually mean? Can you show examples where different `quoting-depth` arguments in `print` lead to different outputs?

**Parameters for `read` and `print`**

I noticed that there are _many_ parameters for the `read` and `print` (starting [here](https://docs.racket-lang.org/reference/Reading.html#%28def._%28%28quote._~23~25kernel%29._read-case-sensitive%29%29) and [here](https://docs.racket-lang.org/reference/Writing.html#%28def._%28%28quote._~23~25kernel%29._print-pair-curly-braces%29%29), respectively). I wonder if all these parameters are actually needed and used?

**Title for the glossary entry**

I'd like to have information on the following in the glossary:

- `print`, `write`, `display` functions
- `format` and `printf` functions
- _placeholders_ like `~a` and `~v` in `format` and `printf`
- _functions_ like `~a`, `~v` and `~r`

At the moment, I tend to put all of this in one entry, possible with subheadings as in [String, character, byte string](https://docs.racket-lang.org/racket-glossary/#%28part._.String_character_byte_string%29), because these concepts/functions are so closely related. Now I wonder what the title for this entry could be. 🙂 Maybe just stick with "Formatting", with other terms, for example "Display", linking to the entry?

**Other feedback**

In addition to the notes that I linked above, is there anything you think I should include in the glossary entry? Remember that the target audience of the glossary are readers which are relatively new to Racket and may be [confused by Racket's many concepts](https://sschwarzer.com/download/glossary_racketfest2023.pdf), so entries in the glossary are intended as an overview/starting point.

---

<div class="post-metadata">

**Author:** ![soegaard](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/soegaard/32/19_2.png) [@soegaard](https://racket.discourse.group/u/soegaard)\
**Post date:** [April 20, 2023, 11:23am UTC](https://racket.discourse.group/t/feedback-for-glossary-entry-on-formatting-print-write-display/1877/2 "2023-04-20T11:23:23Z")

</div>

> [@sschwarzer](#):
>
> As a simple example, I noticed that `(write 'foo)` emits `foo`, not `'foo`, as I had expected.

I think of `quote` as a `'` followed by the written representation of a value, produces that value.  
So the written written representation of symbol with the name `foo` is `foo` and to produce that symbol I write `'foo`.

It is perhaps more clear when lists, are involved: We write `'(foo bar)` not `'('foo 'bar)`.

---

<div class="post-metadata">

**Author:** ![shawnw](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/shawnw/32/1031_2.png) [@shawnw](https://racket.discourse.group/u/shawnw)\
**Post date:** [April 20, 2023, 11:35am UTC](https://racket.discourse.group/t/feedback-for-glossary-entry-on-formatting-print-write-display/1877/3 "2023-04-20T11:35:35Z")

</div>

> [@sschwarzer](#):
>
> As a simple example, I noticed that `(write 'foo)` emits `foo`, not `'foo`, as I had expected.

The quote isn't needed by `read`:

```
(with-input-from-string "foo" read) ; returns the symbol foo

```

---

<div class="post-metadata">

**Author:** ![sschwarzer](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/sschwarzer/32/1940_2.png) [@sschwarzer](https://racket.discourse.group/u/sschwarzer)\
**Post date:** [April 20, 2023, 11:41am UTC](https://racket.discourse.group/t/feedback-for-glossary-entry-on-formatting-print-write-display/1877/4 "2023-04-20T11:41:11Z")

</div>

> [@shawnw](#):
>
> The quote isn't needed by `read`:
> 
> ```scheme
> (with-input-from-string "foo" read) ; returns the symbol foo
> 
> ```

It seems that (in my mental model) `read` "adds" a quote, but I don't understand the reasoning behind that.

```scheme
(with-input-from-string "'foo" read)

```

results in

```scheme
''foo

```

so it's not that the quote in the input is just optional.

---

<div class="post-metadata">

**Author:** ![shawnw](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/shawnw/32/1031_2.png) [@shawnw](https://racket.discourse.group/u/shawnw)\
**Post date:** [April 20, 2023, 11:56am UTC](https://racket.discourse.group/t/feedback-for-glossary-entry-on-formatting-print-write-display/1877/5 "2023-04-20T11:56:48Z")

</div>

`'foo` is sugar for `(quote foo)`, and is read as a list. Lists are one of the things the default printer quotes (Controlled by the `print-as-expression` parameter).

```scheme
> (list? (with-input-from-string "'foo" read))
#t
> (writeln (with-input-from-string "'foo" read)) 
'foo
> (println (with-input-from-string "'foo" read))
''foo
> (parameterize ([print-as-expression #f]) (println (with-input-from-string "'foo" read)))
'foo

```

`read` isn't adding anything; it's the printer that is.

---

<div class="post-metadata">

**Author:** ![sschwarzer](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/sschwarzer/32/1940_2.png) [@sschwarzer](https://racket.discourse.group/u/sschwarzer)\
**Post date:** [April 20, 2023, 11:57am UTC](https://racket.discourse.group/t/feedback-for-glossary-entry-on-formatting-print-write-display/1877/6 "2023-04-20T11:57:15Z")

</div>

> [@soegaard](#):
>
> I think of `quote` as a `'` followed by the written representation of a value, produces that value.  
> So the written written representation of symbol with the name `foo` is `foo` and to produce that symbol I write `'foo`.

I think it actually becomes clearer with a "normal" expression instead of a symbol:

```scheme
(with-input-from-string "(+ 3 4)" read)

```

results in

```scheme
'(+ 3 4)

```

which makes sense. If the code is `(+ 3 4)` and I want to read it as such (but not evaluate it), the result must be `'(+ 3 4)`.

---

<div class="post-metadata">

**Author:** ![jryans](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/jryans/32/31_2.png) [@jryans](https://racket.discourse.group/u/jryans)\
**Post date:** [April 20, 2023, 11:58am UTC](https://racket.discourse.group/t/feedback-for-glossary-entry-on-formatting-print-write-display/1877/7 "2023-04-20T11:58:41Z")

</div>

The distinction between `print`, `write`, and `display` was quite mysterious for me as I was learning Racket (and sometimes I still forget which is which...!).

The most helpful thing for me so far has been reviewing the [Reading and Writing Racket Data](https://docs.racket-lang.org/guide/read-write.html) guide page, especially the outline explaining each type and the table of examples to get a feel for each one in use.

---

<div class="post-metadata">

**Author:** ![sschwarzer](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/sschwarzer/32/1940_2.png) [@sschwarzer](https://racket.discourse.group/u/sschwarzer)\
**Post date:** [April 20, 2023, 12:01pm UTC](https://racket.discourse.group/t/feedback-for-glossary-entry-on-formatting-print-write-display/1877/8 "2023-04-20T12:01:48Z")

</div>

> [@jryans](#):
>
> The most helpful thing for me so far has been reviewing the [Reading and Writing Racket Data](https://docs.racket-lang.org/guide/read-write.html) guide page, especially the outline explaining each type and the table of examples to get a feel for each one in use.

Yes, I found that helpful, too (and mentioned the link above 🙂 ). Likely, I'll use a similar table in the glossary entry, but maybe with additional information. I'm still thinking about how to present the concepts. 🙂

---

<div class="post-metadata">

**Author:** ![alexh](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/alexh/32/315_2.png) [@alexh](https://racket.discourse.group/u/alexh)\
**Post date:** [April 20, 2023, 12:13pm UTC](https://racket.discourse.group/t/feedback-for-glossary-entry-on-formatting-print-write-display/1877/9 "2023-04-20T12:13:12Z")

</div>

> [@sschwarzer](#):
>
> > [@shawnw](#):
> >
> > The quote isn't needed by `read`:
> > 
> > ```scheme
> > (with-input-from-string "foo" read) ; returns the symbol foo
> > 
> > ```
> 
> It seems that (in my mental model) `read` "adds" a quote, but I don't understand the reasoning behind that.

Here is a different way to think about how this works:

- evaluating `(with-input-from-string "foo" read)` returns an internal data structure, a Scheme symbol type, whose name is foo
- you could pass this Scheme symbol to `eval`, who will return the value associated with the symbol (or raise an exception if the symbol `foo` is not bound)
- you could also pass this internal structure to `print` which will create a text representation of this, which is the text `'foo`, which is what you see in the Racket console.

Why does `print` prepend a quote to the symbol name when it is asked to print a symbol? Because `print` assumes the text will be an input to a read- **eval** -print loop. Here is how that would work:

- user types the text `'foo` at the prompt
- **read** reads this text as `(quote foo)` (a list whose first element is the symbol `quote` and the second element is the symbol `foo`
- the `(quote foo)` list is passed to **eval** which evaluates it, producing the Scheme symbol whose name is `foo`
- This Scheme symbol is than passed to **print** which prints it out as the text `'foo`

Alex.

---

<div class="post-metadata">

**Author:** ![samth](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/samth/32/3_2.png) [@samth](https://racket.discourse.group/u/samth)\
**Post date:** [April 20, 2023, 1:51pm UTC](https://racket.discourse.group/t/feedback-for-glossary-entry-on-formatting-print-write-display/1877/10 "2023-04-20T13:51:48Z")

</div>

A different way of phrasing Alex's excellent explanation is:

- `write` is intended to round-trip with `read`. That is, for (some) data `v,` the following code should produce `v` again: `(with-input-from-string (~s v) read)`
- `print` is intended to round-trip with `eval` (although this round-trip is lower-fidelity). That is, the following code produces `v` again: `(with-input-from-string (~v v) (compose read eval))` (or printing v and pasting it into the repl, as Alex said.
- `display` is intended for human consumption only; that is, there is no way to programmatically get back the original data.

---

<div class="post-metadata">

**Author:** ![sschwarzer](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/sschwarzer/32/1940_2.png) [@sschwarzer](https://racket.discourse.group/u/sschwarzer)\
**Post date:** [April 20, 2023, 5:00pm UTC](https://racket.discourse.group/t/feedback-for-glossary-entry-on-formatting-print-write-display/1877/11 "2023-04-20T17:00:24Z")

</div>

Another question:

[8.3&nbsp;Reading and Writing Racket Data](https://docs.racket-lang.org/guide/read-write.html) says:

> In the format string supplied to [printf](https://docs.racket-lang.org/reference/Writing.html#%28def._%28%28quote._~23~25kernel%29._printf%29%29), ~a [display](https://docs.racket-lang.org/reference/Writing.html#%28def._%28%28quote._~23~25kernel%29._display%29%29)s the next argument, ~s [write](https://docs.racket-lang.org/reference/Writing.html#%28def._%28%28quote._~23~25kernel%29._write%29%29)s the next argument, and ~v [print](https://docs.racket-lang.org/reference/Writing.html#%28def._%28%28quote._~23~25kernel%29._print%29%29)s the next argument.

Does anyone know why the letters a, s and v were chosen for the placeholders? Do you know of any mnemonics?

---

<div class="post-metadata">

**Author:** ![soegaard](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/soegaard/32/19_2.png) [@soegaard](https://racket.discourse.group/u/soegaard)\
**Post date:** [April 20, 2023, 5:41pm UTC](https://racket.discourse.group/t/feedback-for-glossary-entry-on-formatting-print-write-display/1877/12 "2023-04-20T17:41:17Z")

</div>

Here are the names used in Common Lisp:

 ![image](https://global.discourse-cdn.com/free1/uploads/racket/original/2X/a/a472a541f697540010e4608e3068b138bee2cca5.png)

a = aesthetic  
s = standard  
v = ?

[http://www.lispworks.com/documentation/HyperSpec/Front/X\_Mast\_9.htm](http://www.lispworks.com/documentation/HyperSpec/Front/X_Mast_9.htm)

---

<div class="post-metadata">

**Author:** ![sschwarzer](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/sschwarzer/32/1940_2.png) [@sschwarzer](https://racket.discourse.group/u/sschwarzer)\
**Post date:** [April 21, 2023, 2:42pm UTC](https://racket.discourse.group/t/feedback-for-glossary-entry-on-formatting-print-write-display/1877/13 "2023-04-21T14:42:44Z")

</div>

If I use `pretty-print`, short data structures (probably those whose output fits on one line?) aren't wrapped. For example, this wraps lines,

```scheme
(define data
  '("This is an example line"
    "Here's another one"
    "And some nested data"
    '(1 2 3)
    "And some more text"))
(pretty-print data)]

'("This is an example line"
  "Here's another one"
  "And some nested data"
  '(1 2 3)
  "And some more text")

```

However, this doesn't wrap:

```scheme
(define data
  '("foo"
    '(1 2 3)
    "bar"))
(pretty-print data)

'("foo" '(1 2 3) "bar")

```

Is there a way to enforce nested indented printing as in the first example, apart from the workaround of setting the `pretty-print-columns` parameter? I realize that a different approach may also split the inner list with the numbers over several lines, but that would be ok (at least in some cases).

---

<div class="post-metadata">

**Author:** ![samth](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/samth/32/3_2.png) [@samth](https://racket.discourse.group/u/samth)\
**Post date:** [April 21, 2023, 2:46pm UTC](https://racket.discourse.group/t/feedback-for-glossary-entry-on-formatting-print-write-display/1877/14 "2023-04-21T14:46:28Z")

</div>

Is what you want that every list of length N appears of N different lines?

---

<div class="post-metadata">

**Author:** ![sschwarzer](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/sschwarzer/32/1940_2.png) [@sschwarzer](https://racket.discourse.group/u/sschwarzer)\
**Post date:** [April 21, 2023, 2:58pm UTC](https://racket.discourse.group/t/feedback-for-glossary-entry-on-formatting-print-write-display/1877/15 "2023-04-21T14:58:55Z")

</div>

> [@samth](#):
>
> Is what you want that every list of length N appears of N different lines?

Yes, that would be an option. Even if this may create "too many" lines, it may be better than the current formatting, depending on the data.

(It may be nice to have an algorithm that tries to wrap and nest lines to maximize readability even if short sub-structures are included, but I'm aware that such "readability" is quite subjective, so this problem may be difficult to solve.)

---

<div class="post-metadata">

**Author:** ![samth](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/samth/32/3_2.png) [@samth](https://racket.discourse.group/u/samth)\
**Post date:** [April 21, 2023, 3:01pm UTC](https://racket.discourse.group/t/feedback-for-glossary-entry-on-formatting-print-write-display/1877/16 "2023-04-21T15:01:12Z")

</div>

I think setting `pretty-print-columns` to 1 is what you want, then.
