Terpri, Prince & co. vs format

Chapter 9.10 Common Lisp: A Gentle Introduction to Symbolic Computation:

The original I / O functions TERPRI, PRIN1, PRINC, and PRINT were defined in Lisp 1.5 (the ancestor of all modern Lisp systems) and are still in Common Lisp today. These are included in the Advanced Topics section as a historical note; you can get the same effect with FORMAT.

This means that you are not using princ

co either . and that in modern code you should only rely on format

.

Are there any disadvantages to doing this? Accordingly, are there any things that cannot be achieved with help format

that works with others?

+3


source to share


2 answers


These functions correspond exactly to the following operators FORMAT

:



  • TERPRI

    = ~%

  • FRESH-LINT

    = ~&

  • PRIN1

    = ~S

  • PRINC

    = ~A

  • PRINT

    = ~%~S<space>

+4


source


You can also use a more modern one write

. I'm not a big fan format

because of its tiny sub-language that is usually interpreted. Note that a good implementation can compile format directives for more efficient code. I use FORMAT mostly when it makes complex code shorter, but doesn't output simple objects or things like single carriage returns ...

Common Lisp includes three or more generations of text I / O interfaces:

  • old s-expression print routines
  • specialized and generic I / O functions
  • complex formatter based on earlier Fortran and / or Multics IO formats
  • General function for printing objects
  • beautiful printer


In addition, there are semi-standard CLOS IO implementations such as Gray Streams.

Everyone can have their own purpose, and no one will leave soon ...

CL-USER 54 > (let ((label "Social security number")
                   (colon ": ")
                   (social-security-number '|7537 DD 459234957324 DE|))

               (terpri)
               (princ label)
               (princ colon)
               (princ social-security-number)

               (write-char #\newline)
               (write-string label)
               (write-string colon)
               (write social-security-number :escape nil)

               (format t "~%~A~A~A" label colon social-security-number)

               )

Social security number: 7537 DD 459234957324 DE
Social security number: 7537 DD 459234957324 DE
Social security number: 7537 DD 459234957324 DE

      

+4


source







All Articles