is it always safe to read a string and converts it back to a string?

318 views
Skip to first unread message

Yehonathan Sharvit

unread,
Nov 20, 2016, 11:53:07 AM11/20/16
to Clojure

Let s be an s-expression in clojure.


Is the following assertion always true?

(= (str (read-string s)) s)




James Reeves

unread,
Nov 20, 2016, 11:59:03 AM11/20/16
to clo...@googlegroups.com
Nope.

=> (str (read-string "\"foo\""))
"foo"

- James


--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Alex Miller

unread,
Nov 20, 2016, 12:40:06 PM11/20/16
to Clojure
pr-str is the companion to read-string

Although, there are many many cases where this round trip will not result in the same input. Things like reader macros, autoresolved keywords, etc will be expanded in the reader.

Yehonathan Sharvit

unread,
Nov 20, 2016, 1:30:58 PM11/20/16
to Clojure
Alex, what is the exact difference between pr-str and str (when passing a single arg)?

On Sun, 20 Nov 2016 at 19:40 Alex Miller <al...@puredanger.com> wrote:
pr-str is the companion to read-string

Although, there are many many cases where this round trip will not result in the same input. Things like reader macros, autoresolved keywords, etc will be expanded in the reader.

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to

For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to a topic in the Google Groups "Clojure" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/clojure/_qzaqqkMHHw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to clojure+u...@googlegroups.com.

Alex Miller

unread,
Nov 20, 2016, 5:29:11 PM11/20/16
to Clojure
Stepping back... Clojure's print system has two families of functions - one for human consumption and one for data. Many kinds of Clojure data print the same in either mode (strings are one exception).

The human printing is most commonly encountered with functions like println, print. and str. It's designed to print things to the repl or to the console for a person to read. Strings print without the surrounding quotes and newlines are really printed as newlines, etc. str doesn't directly use either Clojure printing mode but instead prints the toString() of each object (this is Java's built-in printing system). For strings, both Clojure's printing and str wind up just relying on Java to print a String in a "human-readable" way. Clojure types (like maps) implement toString() to route back into the Clojure printing system. In general, the REPL will use data mode when printing the result of a function (this behavior can be modified). 

The data print functions are things like pr (like print, but for data), prn (like println, but for data), and pr-str (like str, but for data). The idea with the data printers is that the thing you print should be readable by Clojure. So pr-str etc will print a string as the actual characters Clojure would need to read that string back as data.

This is the big picture. I have left the even more complicated pretty printing (pprint) and cl-format (following CommonLisp) parts. You are in a maze of twisty little passages, all alike. If you look too hard at it, you are likely to be eaten by a grue. The print and pprint systems also have many dynamic vars to influence behavior and a number of multimethods intended for extension or modification. In particular, you can provide your own printers for either built-in types or custom records or types by extending things like print-method (human) or print-dup (data). 

If anyone wanted to write a mini Clojure book, this would be a killer topic. 

Alex

On Sunday, November 20, 2016 at 12:30:58 PM UTC-6, Yehonathan Sharvit wrote:
Alex, what is the exact difference between pr-str and str (when passing a single arg)?

On Sun, 20 Nov 2016 at 19:40 Alex Miller <al...@puredanger.com> wrote:
pr-str is the companion to read-string

Although, there are many many cases where this round trip will not result in the same input. Things like reader macros, autoresolved keywords, etc will be expanded in the reader.

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to

For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to a topic in the Google Groups "Clojure" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/clojure/_qzaqqkMHHw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to clojure+unsubscribe@googlegroups.com.

Yehonathan Sharvit

unread,
Nov 24, 2016, 2:10:00 PM11/24/16
to Clojure
Closing the loop: An insteractive blog post made out Alex's detailed explanations: http://blog.klipse.tech/clojure/2016/11/24/stringify-clojure.html

On Mon, 21 Nov 2016 at 00:29 Alex Miller <al...@puredanger.com> wrote:
Stepping back... Clojure's print system has two families of functions - one for human consumption and one for data. Many kinds of Clojure data print the same in either mode (strings are one exception).

The human printing is most commonly encountered with functions like println, print. and str. It's designed to print things to the repl or to the console for a person to read. Strings print without the surrounding quotes and newlines are really printed as newlines, etc. str doesn't directly use either Clojure printing mode but instead prints the toString() of each object (this is Java's built-in printing system). For strings, both Clojure's printing and str wind up just relying on Java to print a String in a "human-readable" way. Clojure types (like maps) implement toString() to route back into the Clojure printing system. In general, the REPL will use data mode when printing the result of a function (this behavior can be modified). 

The data print functions are things like pr (like print, but for data), prn (like println, but for data), and pr-str (like str, but for data). The idea with the data printers is that the thing you print should be readable by Clojure. So pr-str etc will print a string as the actual characters Clojure would need to read that string back as data.

This is the big picture. I have left the even more complicated pretty printing (pprint) and cl-format (following CommonLisp) parts. You are in a maze of twisty little passages, all alike. If you look too hard at it, you are likely to be eaten by a grue. The print and pprint systems also have many dynamic vars to influence behavior and a number of multimethods intended for extension or modification. In particular, you can provide your own printers for either built-in types or custom records or types by extending things like print-method (human) or print-dup (data). 

If anyone wanted to write a mini Clojure book, this would be a killer topic. 

Alex

On Sunday, November 20, 2016 at 12:30:58 PM UTC-6, Yehonathan Sharvit wrote:
Alex, what is the exact difference between pr-str and str (when passing a single arg)?

On Sun, 20 Nov 2016 at 19:40 Alex Miller <al...@puredanger.com> wrote:
pr-str is the companion to read-string

Although, there are many many cases where this round trip will not result in the same input. Things like reader macros, autoresolved keywords, etc will be expanded in the reader.

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to

For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to a topic in the Google Groups "Clojure" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/clojure/_qzaqqkMHHw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to clojure+u...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to

For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to a topic in the Google Groups "Clojure" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/clojure/_qzaqqkMHHw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to clojure+u...@googlegroups.com.

Mike Rodriguez

unread,
Nov 25, 2016, 12:58:25 AM11/25/16
to Clojure
Just FYI. The code part under "Tabs are printed as \t:" has a typo and shows a new line instead of tab.

Otherwise nice work.

Yehonathan Sharvit

unread,
Nov 25, 2016, 4:44:21 AM11/25/16
to Clojure
Thanks Mike.
Fixed

On Fri, 25 Nov 2016 at 07:58 Mike Rodriguez <mjr...@gmail.com> wrote:
Just FYI. The code part under "Tabs are printed as \t:" has a typo and shows a new line instead of tab.

Otherwise nice work.

Reply all
Reply to author
Forward
0 new messages