- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试定义我自己的函数以使我的 hw2 更容易,但它不起作用。你能看一下它并告诉我我缺少什么吗?
(DEFUN testAL(x)
COND ( ( ATOMP(x) ) 'this-is-an-atom )
( ( LISTP(x) ) 'this-is-a-list )
( T 'this-is-neither ) )
我希望这个条件函数接受输入 X 并输出它是原子、列表还是两者都不是。问题是,当我输入 NIL 时,出现错误:Attempt to take the value of the unbound variable `COND'。
家庭作业 2 包含以下问题:
Which of the following are atoms, which lists, which both and witch neither?
a. nil
b. (expt 10 3)
c. (a b)
d. 64
e. T
f. (No place like home)
g. ‘(+ 3 5 6)
最佳答案
您的括号不在正确的位置。除非引用,否则括号是函数应用程序的开始。
它以未绑定(bind)的变量 cond
开头。它不是特殊形式 (cond (predicate consequent) (predicate2 consequent2))
因为它不是以左括号开头。
仅从缩进来看,我猜你打算写:
(DEFUN testAL (x)
(COND ((ATOM x) 'this-is-an-atom)
((LISTP x) 'this-is-a-list)
(T 'this-is-neither)))
(testal 'test) ; ==> THIS-IS-AN-ATOM
(testal '(a b c)) ; ==> THIS-IS-A-LIST
我已经删除了 x
周围的额外括号,因为 (x)
意味着在 x
时应用函数 x
表示变量 x
。如果 x
处于不同的位置,例如 (+ x 3)
,则 +
是要应用的函数,而 x
是其操作数之一。
我将 atomp
更改为 atom
。由于 atom
是 50 年代第一个 LISP 定义的最早原语之一,因此它没有像大多数其他谓词那样的后缀 p
。
编辑:多重匹配
你可以有几个cond
(或if
,因为你在每个中只有一个测试)并产生副作用,比如(print "THIS-IS- AN-ATOM")
因为你的基本情况永远不会触发(在 CL 中没有什么既不是列表也不是原子)。这也许是最简单的解决方案。
(DEFUN testAL (x)
(if (ATOM x) (print 'this-is-an-atom))
(if (LISTP x) (print 'this-is-a-list)))
(testal '()) ; ==> THIS-IS-A-LIST (but prints both)
对于更实用的方法,我会使用高阶函数来完成它,以保持代码可测试并提供一个打印函数来产生副作用。请注意,这对于初学者来说可能不那么容易阅读:
;; a list of pairs of predicate and their desription
(defparameter *type-predicates-and-description*
'((douglasp . this-is-the-answer-to-everything)
(floatp . this-is-a-floating-pont-number)
(integerp . this-is-an-integer)
(numberp . this-is-a-number)
(null . this-is-null)
(listp . this-is-a-list)
(characterp . this-is-a-character)
(stringp . this-is-a-string)))
;; custom made predicate
(defun douglasp (x)
(and (numberp x) (= x 42)))
;; returns all the types of a particular value
(defun get-type-info (x)
"return a list if types thet describes argument"
(flet ((check-type (acc type-pair)
"Accumulate description when predicate match"
(if (funcall (car type-pair) x)
(cons (cdr type-pair) acc)
acc)))
;; test x for each type predicate-description
(let ((res (reduce #'check-type
*type-predicates-and-description*
:initial-value '())))
;; check of empty list (no types matched)
(if (null res)
(list 'this-is-neither)
res))))
;; test it
(get-type-info '()) ; ==> (THIS-IS-A-LIST THIS-IS-NULL)
(get-type-info 42) ; ==> (THIS-IS-A-NUMBER
; THIS-IS-AN-INTEGER
; THIS-IS-THE-ANSWER-TO-EVERYTHING)
(get-type-info #()) ; ==> (THIS-IS-NEITHER)
;; Make a function to do side effects
(defun print-type-info (x)
(format t "~{~a~^, ~}." (get-type-info x)))
(print-type-info '()) ; ==> NIL
; and prints "THIS-IS-A-LIST, THIS-IS-NULL."
关于list - 我定义的 cond 函数无法正常工作 (LISP),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18647427/
我曾尝试在 clojure 中使用 mini-kanren 的实现。但是一直在努力理解 cond-a cond-u 和 cond-e 之间的区别。我似乎很清楚 cond-e 但对 cond-a 和 c
我有一个基本上可以实现的功能 (cond ( (cond) (cond) ) ) 目前计划对我来说似乎是一门外语。我的问题很简单。仅当嵌套条件也返回 true 时,如何
我是 Java 的新手,正在尝试学习迭代器的概念。我在 Java 教程 Oracle 中遇到过这段代码。我的问题是,if (!cond(it.next())) 中的 cond 代表什么。文档没有指定
我正在阅读一个名为 Panda.js 的游戏引擎。核心中有一个方法检查浏览器是否支持全屏,但我不明白这段代码: if (this.system.canvas.requestFullscreen)
我试图了解这两个可观察量之间的区别。代码中唯一的区别是: /** * Inside rxjs pipe */ if(typeof x === 'number' && x > 3) { retu
这两个表达式生成的结果似乎相同。它们之间有什么区别吗? 最佳答案 如果您的 cond 条件为真,但您的 value 不是,则第一个表达式将转到 default_value,而您的第二个表达式只要 co
我正在通过解析类似 Excel 的公式来生成 sql 请求。 因此,对于给定的公式,我收到以下请求: SELECT IF(COL1='Y', SUM(EXPR),NULL) FROM Table 我没
当我执行 np.where(temp == True) 时,PyCharm 会发出警告 我的完整代码: from numpy import where, array a = array([[0.468
是否有任何隐藏的微妙之处,一个是首选,还是一个只是编写另一个的更短方式? Client = db.Clients.First(c=>c.Name == "Client 1") 和 Client = d
if user.item.purchase.status == True: ...使用 flake8 检查时产生错误: E712 comparison to True should be 'if co
我在行“added_parts = new_part_set[(new_part_set["duplicate"] == False) & (new_part_set["version"] ==
1. 简介 本文将介绍 Go 语言中的 sync.Cond 并发原语,包括 sync.Cond 的基本使用方法、实现原理、使用注意事项以及常见的使用使用场景。能够更好地理解和应用 Co
为了在 MongoDB 中对元素进行分组和计数,我已经筋疲力尽了。有很多帖子,但没有一个是我需要的。 这是一个基于以下内容的示例: styvane answer db.VIEW_HISTORICO.a
我正在尝试创建一个计算 状态 我的 Mongo 查询中的字段(状态:已创建、已收到付款、已发货、已收到、已完成)。 db.orders.aggregate( [ { $project: { st
在 Clojure 中的 cond 语句内格式化大型子句的推荐方法是什么? 例如: (cond (> (large-function large-arg1 l
我遇到过很多情况,如果满足特定条件,则需要“更新”两个(甚至三个)值的向量,否则就不用管。示例: (let [val1 some-value val2 some-other-value
scala 有与scheme 的 cond 等价的东西吗? 最佳答案 我猜您正在寻找match(或者只是if/else if/else)。 关于scala - Scala语言中的scheme cond
我正在构建一个 RNN 模型,其中 init_state 可能来自两种情况之一。 1) 通过 feed_dict 从之前的时间步长输出状态输入的静态 init_state。 2) 变量的一些函数,我称
根据https://golang.org/pkg/sync/#Cond.Wait ,必须将 Wait() 调用包装在 for 循环中,因为在第一次恢复时,据称 c.L 没有被锁定。这与上面的句子“..
我正在尝试通过 rmongodb 中的操作在组中执行求和。我求和的“列”包含 NaN 值,导致我的总和返回 NaN。在通过命令运行组之前,我想使用 $project 用零替换 NaN,但我被卡住了。
我是一名优秀的程序员,十分优秀!