鉴于这是一个课程项目,我将提供一个不完整的答案,并让您填写空白。
程序应该做什么
我对您被要求做的事情的解释是提供一个实用程序,它将
- 给定函数的名称,返回其参数列表(下面称为“lambda 列表”);
- 给定一个 lambda 列表,返回具有该 lambda 列表的所有函数。
因此,首先您需要确定两个 lambda 列表是否相同。作为一个例子是(x)
一样的(y)
,作为一个 lambda 列表?是的,它是:形式参数的名称仅在函数的实现中很重要,而且您通常不会知道它们:这两个 lambda 列表都表示“一个参数的函数”。
有趣的是各种可选参数:(a &optional b)
显然与 不同(a)
,但与 相同,(b &optional c)
但是否与 相同(a &optional (b 1 bp))
?在这段代码中,我说是的,它是相同的:可选参数的默认值和当前参数不会改变 lambda 列表是否相同。这是因为这些通常是函数的实现细节。
一袋
我们将它放入一个包中,以便清楚接口是什么:
(defpackage :com.stackoverflow.lisp.fdesc-search
(:use :cl)
(:export
#:defun/recorded
#:record-function-description
#:clear-recorded-functions
#:name->lambda-list
#:lambda-list->names))
(in-package :com.stackoverflow.lisp.fdesc-search)
记录信息
因此,首先我们需要一种记录函数信息的机制。我们将使用一个类似defun
但记录信息的宏来执行此操作,我将其称为defun/recorded
. 我们希望能够在程序存在之前记录有关事物的信息,我们通过defun/recorded
在列表中存储“待处理”记录来做到这一点,一旦程序存在,它将启动并正确记录。这让我们可以defun/recorded
在整个代码中使用。
;;; These define whether there is a recorder, and if not where pending
;;; records should be stashed
;;;
(defvar *function-description-recorder* nil)
(defvar *pending-function-records* '())
(defmacro defun/recorded (name lambda-list &body forms)
"Like DEFUN but record function information."
;; This deals with bootstrapping by, if there is not yet a recording
;; function, stashing pending records in *PENDING-FUNCTION-RECORDS*,
;; which gets replayed into the recorder at the point it becomes
;; available.
`(progn
;; do the DEFUN first, which ensures that the LAMBDA-LIST is OK
(defun ,name ,lambda-list ,@forms)
(if *function-description-recorder*
(progn
(dolist (p (reverse *pending-function-records*))
(funcall *function-description-recorder*
(car p) (cdr p)))
(setf *pending-function-records* '())
(funcall *function-description-recorder*
',name ',lambda-list))
(push (cons ',name ',lambda-list)
*pending-function-records*))
',name))
匹配 lambda 列表,第一步
现在我们希望能够匹配 lambda 列表。由于我们显然要将由 lambda 列表索引的东西存储在某种树中,我们只需要能够处理它们的匹配元素。而且(见上文)我们不关心默认值之类的东西。我选择首先简化 lambda 列表以删除它们,然后匹配 simplifies 元素来做到这一点:还有其他方法。
simplify-lambda-list
进行简化并argument-matches-p
告诉您两个参数是否匹配:有趣的是它需要了解 lambda 列表关键字,这些关键字必须完全匹配,而其他所有内容都匹配任何内容。该lambda-list-keywords
常数由 CL 标准方便地提供。
(defun/recorded simplify-lambda-list (ll)
;; Simplify a lambda list by replacing optional arguments with inits
;; by their names. This does not validate the list
(loop for a in ll
collect (etypecase a
(symbol a)
(list (first a)))))
(defun/recorded argument-matches-p (argument prototype)
;; Does an argument match a prototype.
(unless (symbolp argument)
(error "argument ~S isn't a symbol" argument))
(unless (symbolp prototype)
(error "prototype ~S isn't a symbol" prototype))
(if (find-if (lambda (k)
(or (eq argument k) (eq prototype k)))
lambda-list-keywords)
(eq argument prototype)
t))
功能说明(部分)
有关函数的信息存储在名为fdesc
s 的对象中:这里没有给出这些对象的定义,但我们需要回答的一个问题是“两个fdesc
s 是否指代同一函数的版本?” 好吧,如果函数的名称相同,它们就会这样做。请记住,函数名称不必是符号((defun (setf x) (...) ...)
允许),因此我们必须与equal
not进行比较eql
:
(defun/recorded fdescs-equivalent-p (fd1 fd2)
;; do FD1 & FD2 refer to the same function?
(equal (fdesc-name fd1)
(fdesc-name fd2)))
存储fdesc
由 lambda 列表索引的 s(部分)
为了通过 lambda 列表有效地索引事物,我们构建了一棵树。这棵树中的节点称为lambda-list-tree-node
s,这里不给出它们的定义。
有一些函数fdesc
在树中实习 a,并返回fdesc
由给定 lambda 列表索引的 s 列表。这里都没有实现,但这就是它们的样子:
(defun/recorded intern-lambda-list (lambda-list tree-node fdesc)
;; return the node where it was interned
...)
(defun/recorded lambda-list-fdescs (lambda-list tree-node)
;; Return a list of fdescs for a lambda list & T if there were any
;; or NIL & NIL if there were not (I don't think () & T is possible,
;; but it might be in some future version)
...)
这些功能的实现可能需要使用 useargument-matches-p
和fdescs-equivalent-p
.
顶级数据库(略部分)
现在我们可以定义顶级数据库对象:用于通过 lambda 列表进行索引的树的根,以及用于通过名称进行索引的哈希表
(defvar *lambda-list-tree* (make-lambda-list-tree-node))
(defvar *tree-nodes-by-name* (make-hash-table :test #'equal))
请注意,*tree-nodes-by-name*
从名称映射到存储有关该函数的信息的节点:这样做是为了使重新定义更容易,如下面的函数所示:
(defun/recorded record-function-description (name lambda-list)
"Record information about a function called NAME with lambda list LAMBDA-LIST.
Replace any existing information abot NAME. Return NAME."
(let ((fdesc (make-fdesc :name name :lambda-list lambda-list)))
;; First of all remove any existing information
(multiple-value-bind (node foundp) (gethash name *tree-nodes-by-name*)
(when foundp
(setf (lambda-list-tree-node-values node)
(delete fdesc (lambda-list-tree-node-values node)
:test #'fdescs-equivalent-p))))
(setf (gethash name *tree-nodes-by-name*)
(intern-lambda-list lambda-list *lambda-list-tree* fdesc)))
name)
请注意,此函数首先查找 的任何现有信息name
,如果存在,则将其从找到它的节点中删除。这确保函数重新定义不会在树中留下过时的信息。
这个函数是defun/recorded
想要知道的实际记录器,所以告诉它:
(setf *function-description-recorder*
#'record-function-description)
现在下次我们调用defun/recorded
它时,将通过插入所有未决的定义来引导系统。
record-function-description
是包 API 的一部分:它可用于记录有关我们未定义的函数的信息。
用户界面功能
除了defun/recorded
&record-function-description
我们想要一些让我们查询数据库的函数,以及一个重置事物的函数:
(defun/recorded clear-recorded-functions ()
"Clear function description records. Return no values"
(setf *lambda-list-tree* (make-lambda-list-tree-node)
*tree-nodes-by-name* (make-hash-table :test #'equal))
(values))
(defun/recorded name->lambda-list (name)
"Look up a function by name.
Return either its lambda list & T if it is found, or NIL & NIL if not."
(multiple-value-bind (node foundp) (gethash name *tree-nodes-by-name*)
(if foundp
(values
(fdesc-lambda-list
(find-if (lambda (fd)
(equal (fdesc-name fd) name))
(lambda-list-tree-node-values node)))
t)
(values nil nil))))
(defun/recorded lambda-list->names (lambda-list)
"find function names matching a lambda-list.
Return a list of name & T if there are any, or NIL & NIL if none.
Note that lambda lists are matched so that argument names do not match, and arguments with default values or presentp parameters match just on the argument."
(multiple-value-bind (fdescs foundp) (lambda-list-fdescs lambda-list
*lambda-list-tree*)
(if foundp
(values (mapcar #'fdesc-name fdescs) t)
(values nil nil))))
就是这样。
例子
编译、加载和使用包(添加了缺失的位)之后,我们可以首先向其中注入一些有用的额外功能(这只是随机分散)
> (dolist (x '(car cdr null))
(record-function-description x '(thing)))
nil
> (dolist (x '(car cdr))
(record-function-description `(setf ,x) '(new thing)))
nil
> (record-function-description 'cons '(car cdr))
cons
> (record-function-description 'list '(&rest args))
现在我们可以进行一些查询:
> (lambda-list->names '(x))
(null cdr
car
lambda-list->names
name->lambda-list
com.stackoverflow.lisp.fdesc-search::simplify-lambda-list)
t
> (lambda-list->names '(&rest anything))
(list)
t
> (name->lambda-list 'cons)
(car cdr)
t
将东西存储在树中的示例
下面是一些代码,演示了一种在树中存储信息的方法(通常称为尝试)。 由于很多原因,这在上面不可用,但阅读它可能有助于实现缺失的部分。
;;;; Storing things in trees of nodes
;;;
;;; Node protocol
;;;
;;; Nodes have values which may or may not be bound, and which may be
;;; assigned. Things may be interned in (trees of) nodes with a
;;; value, and the value associated with a thing may be retrieved
;;; along with an indicator as to whether it is present in the tree
;;; under the root.
;;;
(defgeneric node-value (node)
;; the immediate value of a node
)
(defgeneric (setf node-value) (new node)
;; Set the immediate value of a node
)
(defgeneric node-value-boundp (node)
;; Is a node's value bound?
)
(defgeneric intern-thing (root thing value)
;; intern a thing in a root, returning the value
(:method :around (root thing value)
;; Lazy: this arround method just makes sure that primary methods
;; don't need to beother returning the value
(call-next-method)
value))
(defgeneric thing-value (root thing)
;; return two values: the value of THING in ROOT and T if is it present, or
;; NIL & NIL if not
)
;;; Implementatation for STRING-TRIE-NODEs, which store strings
;;;
;;; The performance of these will be bad if large numbers of strings
;;; with characters from a large alphabet are stored: how might you
;;; fix this without making the nodes enormous?
;;;
(defclass string-trie-node ()
;; a node in a string trie. This is conceptually some kind of
;; special case of an abstract 'node' class, but that doesn't
;; actually exist.
((children-map :accessor string-trie-node-children-map
:initform '())
(value :accessor node-value)))
(defmethod node-value-boundp ((node string-trie-node))
(slot-boundp node 'value))
(defmethod intern-thing ((root string-trie-node) (thing string) value)
;; intern a string into a STRING-TRIE-NODE, storing VALUE
(let ((pmax (length thing)))
(labels ((intern-loop (node p)
(if (= p pmax)
(setf (node-value node) value)
(let ((next-maybe (assoc (char thing p)
(string-trie-node-children-map node)
:test #'char=)))
(if next-maybe
(intern-loop (cdr next-maybe) (1+ p))
(let ((next (cons (char thing p)
(make-instance (class-of node)))))
(push next (string-trie-node-children-map node))
(intern-loop (cdr next) (1+ p))))))))
(intern-loop root 0))))
(defmethod thing-value ((root string-trie-node) (thing string))
;; Return the value associated with a string in a node & T or NIL &
;; NIL if there is no value for this string
(let ((pmax (length thing)))
(labels ((value-loop (node p)
(if (= p pmax)
(if (node-value-boundp node)
(values (node-value node) t)
(values nil nil))
(let ((next (assoc (char thing p)
(string-trie-node-children-map node)
:test #'char=)))
(if next
(value-loop (cdr next) (1+ p))
(values nil nil))))))
(value-loop root 0))))
;;; Draw node trees in LW
;;;
#+LispWorks
(defgeneric graph-node-tree (node))
(:method ((node string-trie-node))
(capi:contain
(make-instance 'capi:graph-pane
:roots `((nil . ,node))
:children-function (lambda (e)
(string-trie-node-children-map (cdr e)))
:edge-pane-function (lambda (pane parent child)
(declare (ignore pane parent))
(make-instance
'capi:labelled-line-pinboard-object
:text (format nil "~A" (car child))))
:print-function (lambda (n)
(let ((node (cdr n)))
(format nil "~A"
(if (node-value-boundp node)
(node-value node)
""))))))))