Common Lisp的格式函数里面的\r等价物? [英] Common Lisp's equivalent of \r inside the format function?

查看:187
本文介绍了Common Lisp的格式函数里面的\r等价物?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我想要做以下事情,只使用Common Lisp而不是Python:

  print(Hello world.\r\\\

我可以这样做,但是它只输出#\\\
ewline
字符并跳过#\ return

< pre $ (格式tHello world。〜%)

我相信我可以使用一个外部参数来完成这个工作,比如:

$ p $ (format tHello world。〜C〜% #\ return)

但是对我来说似乎很尴尬。当然,我可以以某种方式嵌入#\ return 到格式字符串中,就像我可以#\ newline

解决方案

返回和换行的字符

\r 是Common Lisp中的字符#\ return



\\\
是Common Lisp中的字符#\ linefeed



以下结束字符串Hello world。带有return和linefeed。

 (格式tHello world。〜C〜C#\ return#\ linefeed)

#\\\
ewline
是平台用作线段的任何东西。在Unix机器上,这通常与#\linefeed 相同。在其他平台(Windows,Lisp Machines,...)上,这可能会有所不同。


$ b 格式控制
FORMAT 控制〜%会打印换行符(!)。


$ b $所以

 (格式tHello world。〜%)

会打印操作系统使用的换行符。 CR或CRLF或LF。根据平台,这将是一个或两个字符。

所以,在Windows机器上您的

<$ p $ (格式tHello world。〜C〜%#\ return)

实际上可能会打印:#\ return #\ return #\\ \\linefeed 。这是三个字符,而不是两个。 Windows使用CRLF换行符。 Unix使用LF。旧Mac OS(在Mac OS X之前)和Lisp Machines使用CR作为换行符。



写入CRLF



如果您确实想要打印CRLF,则必须明确执行此操作。例如:

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

FORMAT 没有用于换行或回车符输出的特殊语法



< FORMAT控件中的换行符


常用的Lisp允许使用多行字符串。因此,我们可以使用它们作为格式控件:

在这里你可以看到控制字符串中的换行符也在输出中:

  CL-USER 77> (格式t〜%第一行
第二行〜%〜%)

第一行
第二行


下面是一个例子,其中〜@ FORMAT控件保持换行,但删除下一行的空格:

  CL-USER 78> (格式t〜%第一行〜@ 
第二行〜%〜%)

第一行
第二行



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?

解决方案

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

这篇关于Common Lisp的格式函数里面的\r等价物?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆