关于 LISP 中关于格式函数的语句的解释
Explaination about a statement in LISP about format function
我必须在 lisp 中将十进制数转换为二进制数。我在网上搜索时遇到了这段代码。
(defun :bits (value &optional (size 64))
(format t "~v,'~B" size value))
所以请解释一下代码的每个属性将做什么。
因此 (format nil "~B" 23)
将以二进制形式输出数字:
> (format nil "~B" 23)
"10111"
但是我们想指定输出字符串的大小,我们可以通过在格式字符串中添加大小作为前缀来实现。
> (format nil "~8B" 23)
" 10111"
但我们不想用空格填充它。我们想用 ~
.
填充它
> (format nil "~8,'~B" 23)
"~~~10111"
现在我们不想在格式字符串中硬编码输出的大小,我们希望将其作为参数传入。这是 ~v 出现的地方:
> (format nil "~v,'~B" 8 23)
"~~~10111"
现在请注意,我将 nil
作为第二个参数传递,而不是 t
。传递 nil 意味着格式化 returns 格式化字符串而不是打印它。您可能更愿意这样做。
describes the actual behavior of the code you're looking at pretty well, but I think it's always worth mentioning where to find the answer, too. The Common Lisp HyperSpec is the best source of Common Lisp documentation, but there are parts of it that are a little bit hard to read. Sometimes the documentation of directives for format 可能有点密集。在这种情况下,您需要查看几个部分,因为示例中的某些内容不仅仅适用于二进制指令 ~B.
你想从 22.3 Formatted Output 开始,它描述了格式字符串的语法:
A directive consists of a tilde, optional prefix parameters separated
by commas, optional colon and at-sign modifiers, and a single
character indicating what kind of directive this is. There is no
required ordering between the at-sign and colon modifier. The case of
the directive character is ignored. Prefix parameters are notated as
signed (sign is optional) decimal numbers, or as a single-quote
followed by a character. For example, ~5,'0d can be used to print an
integer in decimal radix in five columns with leading zeros, or ~5,'*d
to get leading asterisks.
所以我们希望看到一个波浪号,然后是(可选的)用冒号分隔的参数,一个(可选的)at 符号 (@),一个(可选的)冒号 (:),以及实际的前缀指令(哪个区分大小写)。这意味着
~v,'~B
分解为
~ ; initial tilde
v ; prefix parameter (could also be V)
, ; separator between prefix parameters
'~ ; prefix parameter (character following single quote)
B ; directive (could also be b)
所以我们有两个前缀参数:v和~,指令是B.文档中的下一段描述了 v 作为前缀参数时的作用:
In place of a prefix parameter to a directive, V (or v) can be used.
In this case, format takes an argument from args as a parameter to the
directive. The argument should be an integer or character. If the arg
used by a V parameter is nil, the effect is as if the parameter had
been omitted.
现在,要了解 ~B 的一般作用,您需要查看 22.3.2.3 Tilde B: Binary,尽管它几乎会将您重定向到其他地方:
This is just like ~D but prints in binary radix (radix 2) instead of
decimal. The full form is therefore
~mincol,padchar,commachar,comma-intervalB.
该文档描述了可接受的前缀参数(mincol、padchar、commachar 和 comma-interval)。这些是从左到右填写的。例如~v,'B has two of those, so v is mincol and '就是padchar。但我们仍然需要查看 22.3.2.2 Tilde D: Decimal 以了解其中每一个的含义:
~mincolD uses a column width of mincol; spaces are inserted on the
left if the number requires fewer than mincol columns for its digits
and sign. If the number doesn't fit in mincol columns, additional
columns are used as needed.
~mincol,padcharD uses padchar as the pad character instead of space.
… The : modifier causes commas to be printed between groups of digits;
commachar may be used to change the character used as the comma.
comma-interval must be an integer and defaults to 3. When the :
modifier is given to any of these directives, the commachar is printed
between groups of comma-interval digits.
所以,mincol,结果的宽度是v,也就是说会从参数列表中读取,padchar,padding character,是~。因此:
CL-USER> (bits 13 10)
~~~~~~1101 ; 10 characters wide, padded with ~
CL-USER> (bits 1022 10)
1111111110 ; 10 characters wide, no need for padding
CL-USER> (bits 1022 11) ; 11 characters with, padded with ~
~1111111110
我必须在 lisp 中将十进制数转换为二进制数。我在网上搜索时遇到了这段代码。
(defun :bits (value &optional (size 64))
(format t "~v,'~B" size value))
所以请解释一下代码的每个属性将做什么。
因此 (format nil "~B" 23)
将以二进制形式输出数字:
> (format nil "~B" 23)
"10111"
但是我们想指定输出字符串的大小,我们可以通过在格式字符串中添加大小作为前缀来实现。
> (format nil "~8B" 23)
" 10111"
但我们不想用空格填充它。我们想用 ~
.
> (format nil "~8,'~B" 23)
"~~~10111"
现在我们不想在格式字符串中硬编码输出的大小,我们希望将其作为参数传入。这是 ~v 出现的地方:
> (format nil "~v,'~B" 8 23)
"~~~10111"
现在请注意,我将 nil
作为第二个参数传递,而不是 t
。传递 nil 意味着格式化 returns 格式化字符串而不是打印它。您可能更愿意这样做。
你想从 22.3 Formatted Output 开始,它描述了格式字符串的语法:
A directive consists of a tilde, optional prefix parameters separated by commas, optional colon and at-sign modifiers, and a single character indicating what kind of directive this is. There is no required ordering between the at-sign and colon modifier. The case of the directive character is ignored. Prefix parameters are notated as signed (sign is optional) decimal numbers, or as a single-quote followed by a character. For example, ~5,'0d can be used to print an integer in decimal radix in five columns with leading zeros, or ~5,'*d to get leading asterisks.
所以我们希望看到一个波浪号,然后是(可选的)用冒号分隔的参数,一个(可选的)at 符号 (@),一个(可选的)冒号 (:),以及实际的前缀指令(哪个区分大小写)。这意味着
~v,'~B
分解为
~ ; initial tilde
v ; prefix parameter (could also be V)
, ; separator between prefix parameters
'~ ; prefix parameter (character following single quote)
B ; directive (could also be b)
所以我们有两个前缀参数:v和~,指令是B.文档中的下一段描述了 v 作为前缀参数时的作用:
In place of a prefix parameter to a directive, V (or v) can be used. In this case, format takes an argument from args as a parameter to the directive. The argument should be an integer or character. If the arg used by a V parameter is nil, the effect is as if the parameter had been omitted.
现在,要了解 ~B 的一般作用,您需要查看 22.3.2.3 Tilde B: Binary,尽管它几乎会将您重定向到其他地方:
This is just like ~D but prints in binary radix (radix 2) instead of decimal. The full form is therefore ~mincol,padchar,commachar,comma-intervalB.
该文档描述了可接受的前缀参数(mincol、padchar、commachar 和 comma-interval)。这些是从左到右填写的。例如~v,'B has two of those, so v is mincol and '就是padchar。但我们仍然需要查看 22.3.2.2 Tilde D: Decimal 以了解其中每一个的含义:
~mincolD uses a column width of mincol; spaces are inserted on the left if the number requires fewer than mincol columns for its digits and sign. If the number doesn't fit in mincol columns, additional columns are used as needed.
~mincol,padcharD uses padchar as the pad character instead of space.
… The : modifier causes commas to be printed between groups of digits; commachar may be used to change the character used as the comma. comma-interval must be an integer and defaults to 3. When the : modifier is given to any of these directives, the commachar is printed between groups of comma-interval digits.
所以,mincol,结果的宽度是v,也就是说会从参数列表中读取,padchar,padding character,是~。因此:
CL-USER> (bits 13 10)
~~~~~~1101 ; 10 characters wide, padded with ~
CL-USER> (bits 1022 10)
1111111110 ; 10 characters wide, no need for padding
CL-USER> (bits 1022 11) ; 11 characters with, padded with ~
~1111111110