Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

terpri, princ & co. vs format

Chapter 9.10 of Common Lisp: A Gentle Introduction To Symbolic Computation claims:

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

This implies that you do not neet princ & co. any more and that, in modern code, you only should rely on format instead.

Are there any disadvantages when doing this? Respectively, are there any things one can not achieve with format that works with the other ones?

like image 516
Golo Roden Avatar asked Apr 19 '15 15:04

Golo Roden


2 Answers

These functions correspond exactly to the following FORMAT operators:

  • TERPRI = ~%
  • FRESH-LINT = ~&
  • PRIN1 = ~S
  • PRINC = ~A
  • PRINT = ~%~S<space>
like image 128
Barmar Avatar answered Sep 30 '22 17:09

Barmar


You can also use the more modern write. I'm not a huge fan of format because of its terse sub language, which usually is interpreted. Note that a good implementation might be able to compile format directives to more efficient code. I use FORMAT mostly when it makes complex code shorter, but not to output plain objects or things like single carriage returns...

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

  • the old s-expression printing routines
  • the specialized and generalized stream IO functions
  • the complex formatter, based on earlier Fortran and/or Multics IO formatters
  • the Generic Function to print objects
  • the pretty printer

Additionally there are semi-standard CLOS-based IO implementations like Gray Streams.

Each might have its purpose and none is going away 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
like image 39
Rainer Joswig Avatar answered Sep 30 '22 17:09

Rainer Joswig