Spy*_*oft 9 macros common-lisp backquote let-over-lambda
当我编译下面的代码时,SBCL抱怨g!-unit-value和g!-unit是未定义的.我不知道如何调试这个.据我所知,flatten失败了.
当展平到达defunits的未引用部分时,似乎整个部分被视为原子.这听起来不对吗?
以下使用Let over Lambda一书中的代码:
保罗格雷厄姆公用事业
(defun symb (&rest args)
(values (intern (apply #'mkstr args))))
(defun mkstr (&rest args)
(with-output-to-string (s)
(dolist (a args) (princ a s))))
(defun group (source n)
(if (zerop n) (error "zero length"))
(labels ((rec (source acc)
(let ((rest (nthcdr n source)))
(if (consp rest)
(rec rest (cons (subseq source 0 n) acc))
(nreverse (cons source acc))))))
(if source (rec source nil) nil)))
(defun flatten (x)
(labels ((rec (x acc)
(cond ((null x) acc)
((atom x) (cons x acc))
(t (rec (car x) (rec (cdr x) acc))))))
(rec x nil)))
Run Code Online (Sandbox Code Playgroud)
让Over Lambda公用事业 - 第3章
(defmacro defmacro/g! (name args &rest body)
(let ((g!-symbols (remove-duplicates
(remove-if-not #'g!-symbol-p
(flatten body)))))
`(defmacro ,name ,args
(let ,(mapcar
(lambda (g!-symbol)
`(,g!-symbol (gensym ,(subseq
(symbol-name g!-symbol)
2))))
g!-symbols)
,@body))))
(defun g!-symbol-p (symbol-to-test)
(and (symbolp symbol-to-test)
(> (length (symbol-name symbol-to-test)) 2)
(string= (symbol-name symbol-to-test)
"G!"
:start1 0
:end1 2)))
(defmacro defmacro! (name args &rest body)
(let* ((o!-symbols (remove-if-not #'o!-symbol-p args))
(g!-symbols (mapcar #'o!-symbol-to-g!-symbol o!-symbols)))
`(defmacro/g! ,name ,args
`(let ,(mapcar #'list (list ,@g!-symbols) (list ,@o!-symbols))
,(progn ,@body)))))
(defun o!-symbol-p (symbol-to-test)
(and (symbolp symbol-to-test)
(> (length (symbol-name symbol-to-test)) 2)
(string= (symbol-name symbol-to-test)
"O!"
:start1 0
:end1 2)))
(defun o!-symbol-to-g!-symbol (o!-symbol)
(symb "G!" (subseq (symbol-name o!-symbol) 2)))
Run Code Online (Sandbox Code Playgroud)
让Over Lambda - 第5章
(defun defunits-chaining (u units prev)
(if (member u prev)
(error "~{ ~a~^ depends on~}"
(cons u prev)))
(let ((spec (find u units :key #'car)))
(if (null spec)
(error "Unknown unit ~a" u)
(let ((chain (second spec)))
(if (listp chain)
(* (car chain)
(defunits-chaining
(second chain)
units
(cons u prev)))
chain)))))
(defmacro! defunits (quantity base-unit &rest units)
`(defmacro ,(symb 'unit-of- quantity)
(,g!-unit-value ,g!-unit)
`(* ,,g!-unit-value
,(case ,g!-unit
((,base-unit) 1)
,@(mapcar (lambda (x)
`((,(car x))
,(defunits-chaining
(car x)
(cons
`(,base-unit 1)
(group units 2))
nil)))
(group units 2))))))
Run Code Online (Sandbox Code Playgroud)
Rai*_*wig 11
这有点棘手:
问题:您假设反引号/逗号表达式是普通列表.
你需要问自己这个问题:
反引号/逗号表达式的表示形式是什么?
这是一个清单吗?
实际上,完整的表示是未指定的.请参阅此处:CLHS:第2.4.6.1节关于反引号的注释
我们正在使用SBCL.看到这个:
* (setf *print-pretty* nil)
NIL
* '`(a ,b)
(SB-INT:QUASIQUOTE (A #S(SB-IMPL::COMMA :EXPR B :KIND 0)))
Run Code Online (Sandbox Code Playgroud)
所以逗号表达式由类型结构表示SB-IMPL::COMMA.SBCL开发人员认为,当漂亮的打印机需要打印这样的反引号列表时,这种表示会有所帮助.
由于你flatten把结构视为原子,它不会看到内部......
但这是SBCL的具体表现.Clozure CL做了别的事情,LispWorks又做了一些其他事情.
Clozure CL:
? '`(a ,b)
(LIST* 'A (LIST B))
Run Code Online (Sandbox Code Playgroud)
LispWorks:
CL-USER 87 > '`(a ,b)
(SYSTEM::BQ-LIST (QUOTE A) B)
Run Code Online (Sandbox Code Playgroud)
调试
由于您发现flatten涉及某种方式,接下来的调试步骤是:
首先:跟踪函数flatten并查看它被调用的数据和返回的数据.
由于我们不确定数据究竟是什么,所以可以INSPECT.
使用SBCL的调试示例:
* (defun flatten (x)
(inspect x)
(labels ((rec (x acc)
(cond ((null x) acc)
((atom x) (cons x acc))
(t (rec (car x) (rec (cdr x) acc))))))
(rec x nil)))
STYLE-WARNING: redefining COMMON-LISP-USER::FLATTEN in DEFUN
FLATTEN
Run Code Online (Sandbox Code Playgroud)
上面调用INSPECT参数数据.在Common Lisp中,Inspector通常是可以交互式检查数据结构的东西.
作为一个例子,我们flatten使用反引号表达式调用:
* (flatten '`(a ,b))
The object is a proper list of length 2.
0. 0: SB-INT:QUASIQUOTE
1. 1: (A ,B)
Run Code Online (Sandbox Code Playgroud)
我们在交互式Inspector中.现在可用的命令:
> help
help for INSPECT:
Q, E - Quit the inspector.
<integer> - Inspect the numbered slot.
R - Redisplay current inspected object.
U - Move upward/backward to previous inspected object.
?, H, Help - Show this help.
<other> - Evaluate the input as an expression.
Within the inspector, the special variable SB-EXT:*INSPECTED* is bound
to the current inspected object, so that it can be referred to in
evaluated expressions.
Run Code Online (Sandbox Code Playgroud)
所以命令1进入数据结构,这里是一个列表.
> 1
The object is a proper list of length 2.
0. 0: A
1. 1: ,B
Run Code Online (Sandbox Code Playgroud)
走得更远:
> 1
The object is a STRUCTURE-OBJECT of type SB-IMPL::COMMA.
0. EXPR: B
1. KIND: 0
Run Code Online (Sandbox Code Playgroud)
在这里,检查员告诉我们,对象是某种类型的结构.这就是我们想知道的.
我们现在使用命令离开Inspectorq,flatten函数继续并返回一个值:
> q
(SB-INT:QUASIQUOTE A ,B)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
340 次 |
| 最近记录: |