lisp - 格式调用未按要求缩进

标签 lisp common-lisp

我有这个:

(format *standard-output* "~v@a ~a ~%" (* 5 indent) "End of Parent" (* 5 indent))

奇怪的问题是 (* 5 indent)添加为调试项,并且在打印输出中是正确的。但是,在我的函数内部调用时,文本不会缩进,即使它打印出正确的 indent值(value)。如果我只是在 REPL 中执行这一行,它就会正确打印出来,并带有缩进。我的整个功能是这样的:

(defun print-object-and-children-data (object indent)
  "Recursively prints a chart object and its children,
   with each child level indented further."
  (let ((children (get-chart-children object)))
    ;; This indents correctly.
    (format *standard-output* "~v@a Level: ~a~%"
            (* 5 indent) object indent)
    (mapc (lambda (x)
            (when x (print-object-and-children-data x (+ 1 indent))))
          children)
    ;; This does not.
    (format *standard-output* "~v@a ~a ~%"
            (* 5 indent) "End of Parent" (* 5 indent)))) 

所有文字内容均正确。另外,是否最终formatlet 的一部分没有什么区别(它不需要 children 中的任何内容)。此函数中的某些内容以某种方式影响 format 的缩进调用,但由于格式打印出正确的缩进值,那么没有缩进的原因可能是什么?

最佳答案

~v@A 中的 v 没有指定缩进。它指定字段宽度。试试这个:

(dolist (i '(5 10 15))
  (dolist (x '(1 123 12345))
    (format t ">~v@A< (i=~D)~%" i x i)))

请注意输出如何不左对齐。

这是缩进输出的一种可能方法:

(dolist (i '(5 10 15))
  (dolist (x '(1 123 12345))
    (format t ">~vA~A< (i=~D)~%" i " " x i)))

(修复此处的错误留作练习。)

关于lisp - 格式调用未按要求缩进,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20323295/

相关文章:

common-lisp - 通过Lisp地带的封闭示例进行内存

loops - 在 Lisp 中附加到 "loop-collect"的结果

lisp - Common Lisp 中的循环列表

lisp - 来自 On Lisp 的奇异引用列表示例

linux - 我如何使用(需要:PACKAGE) in clisp under Ubuntu Hardy?

tree - UVa #112 树求和

dependencies - 如何管理常见的 lisp 依赖项?

sorting - Common Lisp 连接和换行

macros - 如何将引用的 sexp 传递给宏

lisp - Lisp 和 Scheme 中的 WebSockets 库?