如何在 Lisp 中格式化我的 REPL 输出的数字精度?
How do I format the digit precision of my REPL output in Lisp?
我的问题是:
如何设置我的 REPL 打印输出的精度?
例如,这里的这个简单函数:
(defun gaussian (rows cols sigma)
(let ((filter (make-array `(,rows ,cols)))
(rowOffset (/ (- rows 1) 2.0))
(colOffset (/ (- cols 1) 2.0)))
(loop for i from 0 to (- rows 1)
do (loop for j from 0 to (- cols 1)
do (setf (aref filter i j)
(gaussDistVal i j rowOffset ColOffset sigma))))
filter))
如果我调用 (gaussian 5 5 1)
,我的输出如下:
#2A((0.01831564 0.082085 0.13533528 0.082085 0.01831564)
(0.082085 0.36787945 0.60653067 0.36787945 0.082085)
(0.13533528 0.60653067 1.0 0.60653067 0.13533528)
(0.082085 0.36787945 0.60653067 0.36787945 0.082085)
(0.01831564 0.082085 0.13533528 0.082085 0.01831564))
而我想得到:
#2A((0.0 0.1 0.1 0.1 0.0)
(0.0 0.4 0.6 0.4 0.1)
(0.1 0.6 1.0 0.6 0.1)
(0.0 0.4 0.6 0.4 0.1)
(0.0 0.1 0.1 0.1 0.0))
如果您有答案,能否也请告诉我这些 "REPL customisations" 记录在何处?
(SBCL 1.2.11;Emacs 25 上的史莱姆)
使用 Common Lisp 漂亮的打印机
Common Lisp 有广泛的pretty printer. A rarely used feature is a dispatch table for controlling the printing of objects of a certain type. See set-pprint-dispatch如何配置此功能。
函数format
具有输出各种形式的浮点数的功能。
这个例子结合了两者:
CL-USER 32 > (set-pprint-dispatch 'float
#'(lambda (s obj)
(format s "~,1F" obj)))
NIL
CL-USER 33 > (setf *print-pretty* t)
T
CL-USER 34 > #2A((0.01831564 0.082085 0.13533528 0.082085 0.01831564)
(0.082085 0.36787945 0.60653067 0.36787945 0.082085)
(0.13533528 0.60653067 1.0 0.60653067 0.13533528)
(0.082085 0.36787945 0.60653067 0.36787945 0.082085)
(0.01831564 0.082085 0.13533528 0.082085 0.01831564))
#2A((0.0 0.1 0.1 0.1 0.0)
(0.1 0.4 0.6 0.4 0.1)
(0.1 0.6 1.0 0.6 0.1)
(0.1 0.4 0.6 0.4 0.1)
(0.0 0.1 0.1 0.1 0.0))
可能也想临时用一下:
CL-USER 37 > (let ((*print-pprint-dispatch* (copy-pprint-dispatch)))
(set-pprint-dispatch 'float
#'(lambda (s obj)
(format s "~,1F" obj)))
(pprint #2A((0.01831564 0.082085 0.13533528 0.082085 0.01831564)
(0.082085 0.36787945 0.60653067 0.36787945 0.082085)
(0.13533528 0.60653067 1.0 0.60653067 0.13533528)
(0.082085 0.36787945 0.60653067 0.36787945 0.082085)
(0.01831564 0.082085 0.13533528 0.082085 0.01831564))))
#2A((0.0 0.1 0.1 0.1 0.0)
(0.1 0.4 0.6 0.4 0.1)
(0.1 0.6 1.0 0.6 0.1)
(0.1 0.4 0.6 0.4 0.1)
(0.0 0.1 0.1 0.1 0.0))
我的问题是:
如何设置我的 REPL 打印输出的精度?
例如,这里的这个简单函数:
(defun gaussian (rows cols sigma)
(let ((filter (make-array `(,rows ,cols)))
(rowOffset (/ (- rows 1) 2.0))
(colOffset (/ (- cols 1) 2.0)))
(loop for i from 0 to (- rows 1)
do (loop for j from 0 to (- cols 1)
do (setf (aref filter i j)
(gaussDistVal i j rowOffset ColOffset sigma))))
filter))
如果我调用 (gaussian 5 5 1)
,我的输出如下:
#2A((0.01831564 0.082085 0.13533528 0.082085 0.01831564)
(0.082085 0.36787945 0.60653067 0.36787945 0.082085)
(0.13533528 0.60653067 1.0 0.60653067 0.13533528)
(0.082085 0.36787945 0.60653067 0.36787945 0.082085)
(0.01831564 0.082085 0.13533528 0.082085 0.01831564))
而我想得到:
#2A((0.0 0.1 0.1 0.1 0.0)
(0.0 0.4 0.6 0.4 0.1)
(0.1 0.6 1.0 0.6 0.1)
(0.0 0.4 0.6 0.4 0.1)
(0.0 0.1 0.1 0.1 0.0))
如果您有答案,能否也请告诉我这些 "REPL customisations" 记录在何处?
(SBCL 1.2.11;Emacs 25 上的史莱姆)
使用 Common Lisp 漂亮的打印机
Common Lisp 有广泛的pretty printer. A rarely used feature is a dispatch table for controlling the printing of objects of a certain type. See set-pprint-dispatch如何配置此功能。
函数format
具有输出各种形式的浮点数的功能。
这个例子结合了两者:
CL-USER 32 > (set-pprint-dispatch 'float
#'(lambda (s obj)
(format s "~,1F" obj)))
NIL
CL-USER 33 > (setf *print-pretty* t)
T
CL-USER 34 > #2A((0.01831564 0.082085 0.13533528 0.082085 0.01831564)
(0.082085 0.36787945 0.60653067 0.36787945 0.082085)
(0.13533528 0.60653067 1.0 0.60653067 0.13533528)
(0.082085 0.36787945 0.60653067 0.36787945 0.082085)
(0.01831564 0.082085 0.13533528 0.082085 0.01831564))
#2A((0.0 0.1 0.1 0.1 0.0)
(0.1 0.4 0.6 0.4 0.1)
(0.1 0.6 1.0 0.6 0.1)
(0.1 0.4 0.6 0.4 0.1)
(0.0 0.1 0.1 0.1 0.0))
可能也想临时用一下:
CL-USER 37 > (let ((*print-pprint-dispatch* (copy-pprint-dispatch)))
(set-pprint-dispatch 'float
#'(lambda (s obj)
(format s "~,1F" obj)))
(pprint #2A((0.01831564 0.082085 0.13533528 0.082085 0.01831564)
(0.082085 0.36787945 0.60653067 0.36787945 0.082085)
(0.13533528 0.60653067 1.0 0.60653067 0.13533528)
(0.082085 0.36787945 0.60653067 0.36787945 0.082085)
(0.01831564 0.082085 0.13533528 0.082085 0.01831564))))
#2A((0.0 0.1 0.1 0.1 0.0)
(0.1 0.4 0.6 0.4 0.1)
(0.1 0.6 1.0 0.6 0.1)
(0.1 0.4 0.6 0.4 0.1)
(0.0 0.1 0.1 0.1 0.0))