.comment -*- Mode:TEXT; -*- .comment this lesson and its associate (INPUT) describe basic Lisp I/O. .document OUTPUT - A description of some of the basic Lisp output functions. .tag OUTPUT Lesson OUTPUT, Version 2 Kent M. Pitman, 5/26/79 revised by Victoria Pigman 9/1/82 Lisp has several functions for doing different kinds of output. To output an object in Lisp-readable form, the basic function is PRIN1. (That is, they print in a from that Lisp can understand.) For example, (PRIN1 'FOO) will type FOO on your console. PRIN1's first arg (it has another optional arg but you don't want to worry about it now) is the thing to print. .try To print more than one thing you must either print a list or make two calls to PRIN1. For example: .eval (setq a 'foo b 'bar) (SETQ A 'FOO) (SETQ B 'BAR) (PRIN1 (LIST A B)) or (PROGN (PRIN1 A) (PRIN1 B)) The effects of each are slightly different. (Note - if PROGN is strange to you, there is a lesson on it.) .try The function PRINT is nearly the same as PRIN1, but it spaces things apart from each other by typing a carriage return, then doing PRIN1 of the form and then typing a space. You should do these things and observe the differences... (PRINT A) (PROGN (PRIN1 A) (PRIN1 B)) (PROGN (PRIN1 A) (PRINT B)) (PROGN (PRINT A) (PRIN1 B)) (PROGN (PRINT A) (PRINT B)) .try Note that PRIN1 and PRINT both print things in LISP READABLE FORM (That is, they print in a from that Lisp can understand.) Compare the results of: (PRINT 'A/.B) with the results of (PRINT '|A.B|) .try Did they come out the same? What if you wanted to print that configuration without the vertical bars showing up? You'd use PRINC. PRINC is just like PRIN1 but prints in HUMAN READABLE FORM. Here's a couple to try: (PRINC 'FOO) (PRIN1 'FOO) (PRINC '|This is a test.|) (PRIN1 '|This is a test.|) .try You may also just want to output single characters. One way to do this is by doing (PRINC 'A) for instance (which prints an A). But another way to do it is to use the function TYO. TYO takes a FIXNUM (integer) as an argument and prints the ascii character whose value is that character. So (TYO 65) will print an "A" ... .try There is also a function for printing a carriage return. It is called TERPRI. To see it work try this: (PROGN (PRINC 'A) (PRINC 'B)) (PROGN (PRINC 'A) (TERPRI) (PRINC 'B)) .try Note that now that we know about TERPRI, PRINC, and PRIN1 we realize we didn't need PRINT after all. It could have been defined by doing .pp (DEFUN MYPRINT (X) (TERPRI) (PRIN1 X) (PRINC " ")) Try typing in this function definition and then compare its effects with those of PRINT. .try By the way, many people initially find it confusing that when they type (PRINT ) they see the printed and then they see a T after it. Be aware that the T is not there because of anything PRINT is doing - it's there because everything in Lisp returns values to their caller. If PRINT were called from a program, that program would get back the T, but since it's being called from the terminal, the terminal gets back the T. As you recall, when you do (+ 5 5) you see 10 on your terminal without a print request at all. This is because the 10 is "returned" to the terminal. So when you do (* (+ 5 5) 5) you see only the 50 printed - the 10 is returned to the * operator. So when you do (PRINT 'FOO) and you see: (PRINT 'FOO) FOO T don't be too surprised. Lisp is just returning to the terminal the last value it had ahold of. Try doing (PROGN (PRINT 'FOO) (+ 5 5)) and see if you can figure out what's going on. .try Now for one last useful thing... There's even a function that will tell you how wide a thing will print. FLATC tells you how wide PRINC will print something. FLATSIZE tells you how wide PRIN1 will print it. Example: (FLATC 'FOO) => 3 because PRINC prints it as "FOO" - 3 chars (FLATSIZE 'FOO) is also 3. (FLATC '|This is a test.|)=> 15 because PRINC doesn't print the vertical bars. (FLATSIZE '|This is a test.|)=> 17 because PRIN1 does print vertical bars around the text. .try .next INPUT