gpt4 book ai didi

common-lisp - Allegro CL 中的 pprint

转载 作者:行者123 更新时间:2023-12-03 06:14:26 24 4
gpt4 key购买 nike

我正在尝试用 Common Lisp 编写一个程序来动态创建其他 Lisp 文件。 Common Lisp 的 print 函数对于此目的似乎非常有用。不幸的是,该函数在一行上输出数据。例如(仅打印到标准输出):

(print '(let ((a 1) (b 2) (c 3)) (+ a b c)))
>> (let ((a 1) (b 2) (c 3)) (+ a b c))

生成的 lisp 文件需要是人类可读的,因此不应最小化空格。看来 pprint 函数可以解决我的问题。自 pprint sets *pretty-print* to true ,该函数应打印多行。换句话说:

(pprint '(let ((a 1) (b 2) (c 3)) (+ a b c)))
>> (let ((a 1)
>> (b 2)
>> (c 3))
>> (+ a b c))

但是,在 Allegro CL 中,pprint 的行为方式似乎与 print 相同。输出仅在一行上。有没有办法让函数以“漂亮”的方式打印 s 表达式?在函数正确打印之前是否还需要设置其他全局变量?我正在寻找替代函数/宏吗?感谢您的帮助!

最佳答案

pretty-print 不仅仅由 *print-pretty* 控制。例如,看看与 *print-right-margin* 的交互在 SBCL 中(在 SLIME 下):

CL-USER> (pprint '(let ((a 1) (b 2) (c 3)) (+ a b c)))

(LET ((A 1) (B 2) (C 3))
(+ A B C))
; No value
CL-USER> (let ((*print-right-margin* 10))
(pprint '(let ((a 1) (b 2) (c 3)) (+ a b c))))

(LET ((A
1)
(B
2)
(C
3))
(+ A B
C))
; No value
CL-USER> (let ((*print-right-margin* 20))
(pprint '(let ((a 1) (b 2) (c 3)) (+ a b c))))

(LET ((A 1)
(B 2)
(C 3))
(+ A B C))
; No value

您也许可以通过设置该变量来获得满意的结果,但一般来说您会想看看 22.2 The Lisp Pretty Printer 。 pretty-print 函数有很多地方可以放置可选的换行符等,它们的放置位置取决于很多因素(例如 *print-right-margin* 和 *print-miser-width*)。 22.2.2 Examples of using the Pretty Printer 中有一些使用 pretty-print 格式化 Lisp 源代码的示例。 。内容太多,无法全部引用,但它展示了以下 pretty-print 代码如何根据上下文生成所有这些输出:

(defun simple-pprint-defun (*standard-output* list)
(pprint-logical-block (*standard-output* list :prefix "(" :suffix ")")
(write (first list))
(write-char #\Space)
(pprint-newline :miser)
(pprint-indent :current 0)
(write (second list))
(write-char #\Space)
(pprint-newline :fill)
(write (third list))
(pprint-indent :block 1)
(write-char #\Space)
(pprint-newline :linear)
(write (fourth list))))
 (DEFUN PROD (X Y) 
(* X Y))
(DEFUN PROD
(X Y)
(* X Y))
 (DEFUN
PROD
(X Y)
(* X Y))
 ;;; (DEFUN PROD
;;; (X Y)
;;; (* X Y))

关于common-lisp - Allegro CL 中的 pprint,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24295314/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com