One can certainly debate the merits of the names, and perhaps that's
worth doing, but I'll refrain from it myself, except to point out that
Racket inherited the names "write" and "display" from Scheme, and
"print" is a name that we added.

Here is how to think about these functions:

"write" is a function that is meant to be thought of as a low-tech
serialization tool. It writes out values in a way that matches what
"read" expects so you get back "equal?" results.  So, if you do (write
"x") and you do (write 'x), you want them to look different so they
come properly as either a string or a symbol when read is later used.
Similarly, if you print a string that contains one character, and that
character is the double quote character, i.e., "\"", then you want it
to look just like that, so that you get a one character string
containing that character.

"display" is a function that is useful to produce output for human (or
other tool) consumption. When you "display" a string, you will get the
characters in your string send to the output port, one after the
other. So, if you want the characters x, y, and z to be printed, you
can make a string with those characters and then use display to send
only those characters. Similarly, if you (display "\""), you'll get
one character sent to the output, namely the double quote character.

Finally "print" is intended to be a language-specific printing
mechanism. If you use some "#lang crazy-lang" that has its own
idiosyncratic way of printing out values for programmers to debug,
then that language would change what "print" does and if you wanted to
see the values in that way, you'd use write. For example, in #lang
racket, "print" is doing the same thing as what you see at the repl,
ie.e (print (list 1 2)) is the same thing as (display "(list 1 2)"),
well, perhaps with a newline at the end I suppose.

hth,
Robby

PS: When you use format or printf, the "~s" escape means "use write"
and the "~a" escape means "use display".



On Sat, Feb 17, 2018 at 11:56 AM, Zelphir Kaltstahl
<zelphirkaltst...@gmail.com> wrote:
>
> I am writing to a file using the following procedure:
>
> ~~~
> ;; Writes a list of strings as lines to a file.
> (: write-to-file (-> Path-String (Listof String) Void))
> (define (write-to-file path string-list)
>   (call-with-output-file path #|#:mode 'text|# #:exists 'append
>     (λ ([output-port : Output-Port])
>       (printf "Output port: ~s~n" output-port)
>       (for ([line string-list])
>         #;(write-bytes (string->bytes/utf-8 (format "~s~n" line))
> output-port)
>         #;(write-string (format "~s~n" line) output-port)
>         (display line output-port)
>         (parameterize ([print-as-expression #t])
>           (print line output-port 0))
>         #;(printf "~s~n" line output-port)
>         #;(write line output-port)
>         #;(write newline output-port)))))
> ~~~
>
> Writing to a file using display or displayln works as I wanted to write, but
> all other ways I tried add double quotes for strings which I am writing, so
> that the double quotes appear in the output file. I also tried some stuff
> like string->bytes/utf-8 and write-bytes but that did not change that double
> quotes get into the file.
>
> The docs here seem to say that I can make print output to a an output port
> any way I want and if it is not too complicated, I might want to use print
> instead of display to write to a file, for naming reasons. print sounds more
> like something that is actually writing to a file than display, at least to
> me.
>
> How would I make print behave like display?
>
> --
> You received this message because you are subscribed to the Google Groups
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to racket-users+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to