terpri, princ & co. vs format
Asked Answered
S

2

7

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?

Saiz answered 19/4, 2015 at 15:4 Comment(2)
No, you should not rely instead of format and that is not implied. Just because you can use something newer doesn't mean that it is always better.Blissful
I don't know that this merits a whole answer, but I find TERPRI particularly helpful in writing pretty printing routines, where I know that I just want a newline. It feels like (format ... "~%") is just overkill when I know exactly what I need and I don't need format to interpret it for me. Also FRESH-LINE as opposed to (format ... "~&").Parodic
S
11

These functions correspond exactly to the following FORMAT operators:

  • TERPRI = ~%
  • FRESH-LINT = ~&
  • PRIN1 = ~S
  • PRINC = ~A
  • PRINT = ~%~S<space>
Stockyard answered 19/4, 2015 at 15:12 Comment(5)
It exists some function which is like "~A~%" ? (a modern print)Gca
@ManoelVilela I don't think so. WRITE is the most general output function, but I don't think any of its options do that.Stockyard
Thanks for answering it. It's a funny thing for me because is so common having a print function like that on other languages that I almost missed that. You can really use PRINC + TEPRI or even FORMAT to accomplish that result, but I missed CL stdlib to have a function like PRINTLN. But never mind, it's so easy to write one that doesn't worth discuss about it. It's just a funny thing for me.Gca
I think the historical reason for PRINT writing the newline at the beginning was because the original MACLISP started executing in the REPL as soon as you typed the closing ), without requiring you to press Return. So this ensured that output would be on the next line.Stockyard
I didn't know that, nice information! This seems a good reason.Gca
N
10

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
Nonconformity answered 19/4, 2015 at 15:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.