Common Lisp's equivalent of \r inside the form

2019-03-09 07:01发布

Basically, I'd like to do the following, only using Common Lisp instead of Python:

print("Hello world.\r\n")

I can do this, but it only outputs the #\newline character and skips #\return:

(format t "Hello world.~%")

I believe I could accomplish this using an outside argument, like this:

(format t "Hello world.~C~%" #\return)

But is seems awkward to me. Surely I can somehow embed #\return into the very format string, like I can #\newline?

2条回答
戒情不戒烟
2楼-- · 2019-03-09 07:47

Characters for return and linefeed

\r is the character #\return in Common Lisp.

\n is the character #\linefeed in Common Lisp.

The following ends the string "Hello world." with return and linefeed.

(format t "Hello world.~C~C" #\return #\linefeed)

#\newline is whatever the platform uses as a line division. On Unix machines this is often the same as #\linefeed. On other platforms (Windows, Lisp Machines, ...) this could be different.

FORMAT control

The FORMAT control ~% prints a newline (!).

So

(format t "Hello world.~%")

will print the newline that the operating system uses. CR or CRLF or LF. Depending on the platform this will be one or two characters.

So, on a Windows machine your

(format t "Hello world.~C~%" #\return)

might actually print: #\return #\return #\linefeed. Which is THREE characters and not two. Windows uses CRLF for newlines. Unix uses LF. Old Mac OS (prior to Mac OS X) and Lisp Machines used CR for newlines.

Writing CRLF

If you really want to print CRLF, you have to do it explicitly. For example with:

(defun crlf (&optional (stream *standard-output*))
  (write-char #\return stream)
  (write-char #\linefeed stream)
  (values))

FORMAT does not have special syntax for output of linefeed or carriage return characters.

Linebreaks in FORMAT control

Common Lisp allows multi-line strings. Thus we can use them as format controls:

Here you can see that the line break in the control string is also in the output:

CL-USER 77 > (format t "~%first line
second line~%~%")

first line
second line

NIL

Here is an example where the ~@ FORMAT control keeps the linebreak, but removes the whitespace on the next line:

CL-USER 78 > (format t "~%first line~@
                          second line~%~%")

first line
second line

NIL
查看更多
相关推荐>>
3楼-- · 2019-03-09 07:51

First, in Common Lisp most characters, including return/newline, can be inserted directly into the string. The only character requiring escaping is the string delimiter.

There is also a library cl-interpol which provides a read macro to construct strings with more complex syntax, including special character escapes.

查看更多
登录 后发表回答