- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试调整此 defstruct例如,添加本书中描述的 select- 函数:Practical Common Lisp .我使用 Common Lisp 包在 Emacs 中运行代码。 select-by-first 不返回任何内容。在 Lisp 书中,作者没有使用 defstruct 所以我必须做一些稍微不同的事情?
(defun select-by-first (first-name) (remove-if-not #'(lambda (employee) (equal (getf employee :first-name) first-name)) *emp-db*))(select-by-first "steve")
完整程序:
(require 'cl)
;; http://mypage.iu.edu/~colallen/lp/node56.html
;; http://www.gigamonkeys.com/book/practical-a-simple-database.html
;;
(defvar *emp-db* nil)
(defun add-record (emp) (push emp *emp-db*))
(defstruct employee
age
first-name
last-name
sex
children)
(add-record (make-employee))
(add-record (make-employee
:age 34
:last-name 'farquharson
:first-name 'alice
:sex 'female))
(add-record (make-employee
:age 43
:last-name 'jobs
:first-name 'steve
:sex 'male))
(add-record (make-employee
:age 53
:last-name 'ballmer
:first-name 'steve
:sex 'male))
(defun select-by-first (first-name)
(remove-if-not
#'(lambda (employee)
(equal (getf employee :first-name) first-name))
*emp-db*))
(select-by-first "steve")
最佳答案
有一些基本的错误/问题。但只需进行两个小的更改,我们就可以让您的示例在 Common Lisp 中运行。
Emacs Lisp 的 Common Lisp 兼容包并不是真正的 Common Lisp。通常最好使用真正的 Common Lisp 实现。 Emacs Lisp 缺少一些难以模仿的基本内容以使其与 Common Lisp 兼容 - 例如词法闭包(2014 年更新,最新版本的 GNU Emacs 现在也支持词法闭包)。
小改动:我更改了您的示例,这样数据库就不再包含 Steve Jobs 两次,而是包含 Steve Jobs 和 Steve Ballmer。
现在,我们需要更改什么才能使其在 Common Lisp 中工作?
(getf employee :first-name) 实际上应该是 (employee-first-name employee) 。 DEFSTRUCT 宏自动生成这些访问器函数。在 Common Lisp 中,您不能使用 GETF 访问真实结构的字段。
您的数据库有两个名为 STEVE(符号)的对象,但您要搜索名称“steve”(字符串)。 (等于'史蒂夫“史蒂夫”)是错误的。通常,符号不等于字符串。因此,您应该使用 (select-by-first 'steve) 进行搜索。
然后在 LispWorks 中:
CL-USER 11 > (select-by-first "steve")
NIL
CL-USER 12 > (select-by-first 'steve)
(#S(EMPLOYEE :AGE 53 :FIRST-NAME STEVE :LAST-NAME BALLMER :SEX MALE
:CHILDREN NIL)
#S(EMPLOYEE :AGE 43 :FIRST-NAME STEVE :LAST-NAME JOBS :SEX MALE
:CHILDREN NIL))
关于elisp - 使用带有 remove-if-not 的 defstruct 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2182210/
在 CLHS 中,我为 :read-only x 读到:“当 x 为真时,这指定不能更改此插槽;它将始终包含构造时提供的值。” 我可以做到这一点(CCL、SBCL): CL-USER> (defstr
我想将数据保存到文件中,并在重新运行程序时恢复数据。 我已经这样定义了我的 defstruct: (defstruct书签:url:标题:评论) 程序将简单地执行以下操作: 1. 从 url-db.t
是否可以像函数一样定义结构的槽之一并访问槽以使用该函数?如果是,如何使用它?例如,像这样: (defstruct problem state (player (defun getplaye
在普通的 lisp 中,我注意到我可以这样写: (defun foo (&key (a 1) (b 2) (c (+ a b))) (print (+ a b c))) 当我打电话时 (foo) ,
我正在尝试将 defstruct 的实例嵌套为另一个 defstruct 中的一些 &key 参数,如下面的代码所示。出于某种原因,要插入外部 defstruct 的变量不会被绑定(bind)到它们的
我在 Lisp 中定义了一个非常简单的数据结构: ;;Data structure for a person (defstruct person (name nil) (age 0) (s
我正在尝试调整此 defstruct例如,添加本书中描述的 select- 函数:Practical Common Lisp .我使用 Common Lisp 包在 Emacs 中运行代码。 sele
我正在尝试调整此 defstruct例如,添加本书中描述的 select- 函数:Practical Common Lisp .我使用 Common Lisp 包在 Emacs 中运行代码。 sele
我正在自学 Common Lisp。我一直在看康威生命游戏的例子,有一段语法我不明白。 完整代码可用here .我特别遇到问题的部分如下: (defstruct (world (:constructo
ClojureScript 不支持 defstruct - 这似乎是设计使然。现在这可能实际上是 Clojure 语言中已弃用的部分,ClojureScript 的设计者只是希望每个人都继续前进。 (
问题:如何为析构(以及实例)的嵌套槽值编写通用访问器宏? 动机:我是一名 LISP 程序员,羡慕 Python 等中的“点符号”,其中嵌套槽访问距离只有几个点 用例:我想做的是 (print (??
是否有比以下更简单的方法来测试符号是否是结构的名称: (fboundp 'make-symbol) 最佳答案 (defun symbol-names-structure-p (symbol) (l
在常见的 lisp 中,我可以使用什么来使用插槽名称/符号访问结构插槽? 我想要的是 (defstruct point (x 0) (y 0)) (defmacro -> (struct slo
假设我们有上面的代码: (require-extension defstruct) (defstruct tree height age leaf-color) (define coconut (
我了解如何使用 @type 指令来定义用户定义的类型。到目前为止,我只在函数规范中遇到过它的用法。我很想知道它的用法是否可以扩展到结构,使参数与其数据类型之间存在紧密耦合。 我开始探索一个示例程序。
我是一名优秀的程序员,十分优秀!