[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [plt-scheme] Printf and display



Quoth Paulo J. Matos:
> What's the difference between:
> (printf "x")
> (display "x")
> 
> It's because of:
> ~a or ~A displays the next argument among the vs
> ~v or ~V prints the next argument among the vs
> 
> what's the big difference?

The short answer: when you are just outputting simple text (as in your
example), there is no difference.

The long answer:
printf is short for "print formatted", and is so named because there
is a well-known C function of the same name with vaguely similar
semantics.  The idea of printf is that you give it a "format string",
which is a mixture of plain text and formatting commands, and a series
of expressions to be printed.  Each formatting command in the format
string corresponds to exactly one expression to be printed, and
specifies how said printing is done.  A formatting command is a tilde
(~) followed by a letter (aside: C uses % for this purpose).

Example: (printf "~a foo ~a" 3 (+ 2 4)) would print "3 foo 6".

There are a few different simple output commands in mzscheme:
display, print, and write.  With some arguments (e.g. numbers) they
output the same things, while with others (e.g. strings) their output is
different.  I'll let you read the manual and experiment to see the exact
differences.

The three formatting commands for printf (~a, ~s, ~v) each correspond
to one of these simple output commands.  For arguments where the
commands do the same thing (e.g. numbers), it doesn't matter which you
use.  (printf "~a" 3) and (printf "~s" 3) do the same thing.  For other
arguments it does matter: the difference between (printf "~a" "foo") and
(printf "~s" "foo") is that the latter will have the word "foo"
surrounded by double quotes, and the former will output it without the
double quotes.

So, to finally answer your question, (display "x") corresponds more
directly to (printf "~a" "x"); the difference comes to bear in practice
when you are dealing with arguments other than literal strings.

-- 
-=-Don Blaheta-=-=-dpb@cs.brown.edu-=-=-<http://www.cs.brown.edu/~dpb/>-=-
"FOR THE LOVE OF ST ISIDORE WHEN WILL TEMPLATES FINALLY WORK?"
							--Scott Swanson