我正在尝试用普通的lisp构建一个二进制搜索树。我已经使用CLOS定义了二进制搜索类,如下所示:
(defclass bst ()
((root :type node
:accessor tree-root
:initform nil
:initarg root)))我正在尝试定义一个泛型函数,它接受tree对象和一个键,如果树包含键,则返回布尔值true;如果树不包含键,则返回nil。
现在我有一个泛型函数的定义如下:
(defgeneric contains ((tree bst) (key))
(:documentation "returns boolean of whether the given tree contains a particular key)当我将文件加载到REPL (我使用的是SBCL)时,我得到了以下错误:
Required argument is not a symbol: (TREE BST)我是不是误解了泛型函数的工作原理?我似乎不能正确地定义函数。
发布于 2016-09-01 00:09:31
是的,defgeneric定义了一个泛型函数。您可以在调用defgeneric时指定方法,也可以使用defmethod指定方法。
您需要执行以下操作之一:
(defgeneric contains (tree key)
(:documentation "returns boolean of whether the given tree contains a particular key")
(:method ((tree bst) key) ...))或者:
(defgeneric contains (tree key)
(:documentation "returns boolean if a given tree contains a given key"))
(defmethod contains ((tree bst) key)
...)https://stackoverflow.com/questions/39253047
复制相似问题