-
Chapter 6. Printing, Reading, and Debugging
- University of Michigan Press
- Chapter
- Additional Information
74 Chapter 6 Printing, Reading, and Debugging This chapter introduces you to writing output, reading data from the computer keyboard, and debugging programs. Displaying information on your monitor is very helpful in locating problems in your programs. 6.1 print Command We have already seen the print command, which causes expressions to be evaluated and printed. The print template is print expression1, expression2, … print will print a constant, symbol, string, or the value of a variable or expression. A newline is printed first; then each value is printed followed by a space. Example 6.1.1: The print command SAL> print 45 45 SAL> print quote(a-symbol) A-SYMBOL SAL> print "hello world!" hello world! SAL> print *default-sound-srate* 44100 SAL> print 2 + 3 + 4 9 6.2 format Command The function format allows you greater control of the formatting of your output. The format template is format(#t, format-control-string, things-to-print) format is followed by true (#t) to indicate that we want to print to the monitor. (Later we will see how to direct output to a file.) A string is used as the format-control-string. Since format is a 6.2 format Command 75 function, we use an exec command to call it. format is not a command like print. Example 6.2.1: format SAL> exec format(#t, "I love Nyquist!") I love Nyquist! The format-control-string is always enclosed in double quotes. The format-control-string may include format directives – special characters in the format-control-string that cause output to appear in certain ways. format directives always begin with the tilde (~). Table 6.2.1 gives an overview of the most useful format directives. Table 6.2.1: format directives format Directive Result ~% Move to a new line ~A Print the value of a variable or expression ~S Print the value of a variable or expression using quotes around strings ~~ Print the tilde character Example 6.2.2: The ~% format directive SAL> exec format(#t, "~%this is the first line ~%and this is the second") this is the first line and this is the second Example 6.2.3: The ~A format directive SAL> set count = 43 SAL> exec format(#t, "The count is ~A~%", count) The count is 43 Notice that in Example 6.2.3 the characters “~A” in the format string are replaced by the value of count which appears as an input to format. In Example 6.2.4, “~A” appears twice in the format string, so there are a total of four input parameters. Also, note that error-msg is inserted into the formatted output without quotation marks. Example 6.2.5 is identical to 7.2.4 except that “~S” is used in the format string. Notice the output is similar, but the printed version of error-msg includes quotation marks. [44.220.41.140] Project MUSE (2024-03-28 13:14 GMT) 76 Chapter 6 ⋅ Printing, Reading, and Debugging Example 6.2.4: format multiple items SAL> set error-msg = "you entered a negative number" SAL> set error-value = -7 SAL> exec format(#t, "Error: ~A, ~A", error-msg, error-value) Error: you entered a negative number, -7 Example 6.2.5: ~S format directive SAL> set error-msg = "you entered a negative number" SAL> set error-value = -7 SAL> exec format(#t, "Error: ~S, ~A", error-msg, error-value) Error: "you entered a negative number", -7 6.3 display Command “Debugging” is the process of finding errors (bugs) in your programs. When debugging, it often helps to print out values of variables and expressions to confirm that the program is operating as expected. Printing makes the computation process more observable. Although format is a perfectly usable function for debugging, a special command, display, can be even easier. The first input to display is a string that is simply printed. After that, any expression is printed along with its value. Example 6.3.1: display SAL> display "display example", *default-sound-srate*, count Display example : *DEFAULT-SAMPLE-RATE* = 44100 COUNT = -7 SAL> exec format(#t, "Format example : *default-sound-srate* = ~A count = ~A~%", *default-sound-srate*, count) Format example:: *default-sample-rate* = 44100 count = -7 In Example 6.3.1, equivalent output is generated using display and format. Notice the display version is more compact. 6.4 Debugging with #display and #print 77 6.4 Debugging with #display and #print Sometimes, it may be useful to monitor the values of one...