- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
请注意以下宏正在运行。
(define-syntax xor
(syntax-rules ()
((xor a1 a2)
(if a1
(false? a2)
(true? a2)))
((xor a1 a2 a3 ...)
(let ((a (xor a1 a2)))
(xor a a3 ...)))))
不过,一般情况下参数列表中的a3
似乎是不能正常工作的。
(define-syntax xor
(syntax-rules ()
((xor a1 a2)
(if a1
(false? a2)
(true? a2)))
((xor a1 a2 ...)
(let ((a (xor a1 a2)))
(xor a ...)))))
因此我想知道第二种情况下的省略号到底发生了什么。
Q1.这是否意味着除了省略号之外的每个参数列表都应该是唯一的才能正常运行?
例如给定输入(xor #t #t #t)
,第一个将产生#t
,而第二个将产生#f
.
我使用的解释器是mit-scheme
。
Q2.有没有可能让它短路?
谢谢,
最佳答案
省略号告诉您有关第二个符号的信息。 a2 ...
可以是零个或多个元素,您需要在重复的内容中使用 a2
才能使其正常工作。在你的第二个宏中, a2
的结果缺少重复元素的省略号,你在 a
之后有 ellipsis
这不是匹配模式,也没有省略号。这两个事实都使宏无效。
第一个宏是正确的,因为您有一个术语匹配两个元素。您的第二个术语也匹配两个术语,但是由于运行了第一个匹配的模式,您确定您的第二个模式有两个以上的参数,因为它匹配两个 a3 ...
至少是一个元素。
我不确定 true?
的用途。稍微简化一下:
(define-syntax xor
(syntax-rules ()
((xor a1 a2)
(if a1 (not a2) a2))
((xor a1 a2 a3 ...)
(xor (xor a1 a2) a3 ...))))
(xor 1 2 3 4 5) ; ==> 5 (odd number of true values)
(xor 1 2 3 4) ; ==> #f (even number of true values)
(xor 1 2 3 4 #f) ; ==> #f (even number of true values)
(xor 1 #f #f #f) ; ==> #t (odd number of true values)
现在这将计算参数表达式的奇校验。它不能短路,因为它是触发器。 (xor #t #t #f#f#t) ; ==> #t
因为它有奇数个真实参数。这就是它的作用,虽然它是菊花链 xor
逻辑,但它实际上并没有剩下唯一的一个真正的逻辑。由于您永远无法将其短路,因此您不妨使用执行完全相同操作的过程:
(define (xor . args)
(= (remainder (count values args) 2) 1))
(xor 1 2 3 4 5) ; ==> #t (odd number of true values)
(xor 1 2 3 4) ; ==> #f (even number of true values)
(xor 1 2 3 4 #f) ; ==> #f (even number of true values)
(xor 1 #f #f #f) ; ==> #t (odd number of true values)
计数可以在 SRFI-1 list library 中找到.
xor
还有另一种解释,这是我读到这个问题时想到的第一个解释,因为这是短路起作用的唯一情况。当且仅当一个表达式为真时,它为真,否则结果为假。在这里,当遇到第二个假值时,您可以短路到 #f
而无需评估其余参数。
(define-syntax xor
(syntax-rules ()
((_) #f)
((_ a) a)
((_ a b ...)
(if a
(not (or b ...))
(xor b ...)))))
(xor 1 2 3 4 5) ; ==> #f (more than one true value)
(xor 1 2 3 4) ; ==> #f (more than one true value)
(xor 1 2 3 4 #f) ; ==> #f (more than one true value)
(xor 1 #f #f #f) ; ==> #t (only one true value)
;; Slightly more complex version where
;; the result is always the one true value or #f
(define-syntax xor
(syntax-rules ()
((_) #f)
((_ a) a)
((_ a b ...)
(let ((tmp a))
(if tmp
(and (not (or b ...)) tmp)
(xor b ...))))))
(xor 1 2 3 4 5) ; ==> #f
(xor 1 2 3 4) ; ==> #f
(xor 1 2 3 4 #f) ; ==> #f
(xor 1 #f #f #f) ; ==> 1 (the actual true value, consistent)
大多数算法不会因使用过程而有任何速度损失,但我想在某些情况下,此宏可能会像宏一样派上用场。一个不保留值的程序版本与另一个程序版本非常相似:
(define (xor . args)
(= (count values args) 1))
关于macros - 为 xor 定义方案宏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38944947/
如何在 Excel 中编写可以在我将打开的任何 Excel 文档上工作(使用快捷方式运行)的宏? 这可能吗? 最佳答案 您需要将宏添加到 Personal.xlsb 以使它们可用于所有 excel 文
我正在研究 problem #74在4clojure.com,我的解决方案如下: (defn FPS [s] (->> (map read-string (re-seq #"[0-9]+"
我还没有完全理解Clojure 箭头宏thread-first -> 和thread-last 宏->> 之间的区别。在阅读 https://clojure.org/guides/threading_
我想将一些调试输出语句插入到大型 C 代码库中。这些调试输出语句将由编译器选项开关控制。 调试输出语句如下所示: #ifdef DEBUG_FLAG Print(someSymbol) #endif
我正在通过宏将代码注入(inject)到 C++ 类中。有没有办法根据访问修饰符的上下文来做到这一点?有点像 #if (we_are_in_public_context) INJECT_PUBLIC_
这应该与 memoize 类似,但有很大不同。虽然 memoize 应该与纯函数一起使用,但它通常对加速 IO 相关函数很有用。 我正在寻找的函数/宏应该表现得像高阶函数。它产生的功能应该: 第一次调
对于下面的代码: let services: [MyServices] = [ MyService(), #if DEBUG DebugService(), #endi
假设我有以下文本文件 name: John Doe description: My name is John Doe and I'm really good at vim! name: John Do
在创建 Excel 宏方面需要帮助。我有一个 Excel 工作表。Excel 工作表不一致。我打算使它统一和结构化。 例如。 A B C
我正在 excel 中设置一个宏,以便在更新单元格时自动发送电子邮件。是否可以在电子邮件正文中包含单元格的内容?例如,如果单元格 G7 已更新,请在电子邮件中包含单元格 B7 的内容?单元格行将是相同
我创建了一个简单的 Excel 工作表。 这是我的宏代码: Sub MyMacro() Sheets("Sheet1").Select A$ = Cells(1, 1) Msg
在 Excel 的 VB 宏中,如何删除所有出现的以某个字符串开头的单词? 例如: 字符串内容为:xxxx $AUD543.43 yyyy 我想搜索以 $AUD 开头的字符串中的任何内容并删除下一个空
我是 Excel 宏的新手.. 谁能告诉我这个宏是做什么的? Sub People_Add_Document() prow = ActiveCell.row num = Cells(p
我对 Excel 中的 VBA 和宏非常陌生。我有一个非常大的 Excel 电子表格,其中 A 列保存日期。我正在尝试删除值小于某个日期的行,这就是我到现在为止的想法。 Sub DELETEDATE(
我在 Excel 2003 中有一个 VBA 对象,当通过流数据获得某些值时,它会触发三个简单的宏。它运行良好。我想打开一个重复的工作表,但具有不同的流数据,并在各自的工作表上触发宏。它现在可以使用,
下面的宏有什么问题?我只想评估一个选项卡中的一个单元格是否大于另一个选项卡中的另一个单元格。然后消息框: Sub Comhouse() If Worksheets("(2.2) TRA works
需要一个简单的 excel 宏的帮助。我在第 1 列 X1 到 X20 中有数据。我想自动将此信息粘贴到 A 列,然后当我更新 X 列中的数字时,我想将此信息粘贴到 B 列,然后再粘贴到 C 列...
我找到了以下代码,效果很好;但是,我必须手动更改月份,以便它转到第二个工作簿的右侧工作表。由于工作表以月为单位,我怎样才能使其自动更改为当月? Sub AlarmSheet() Dim wkb As
很难说出这里问的是什么。这个问题是模棱两可的、模糊的、不完整的、过于宽泛的或修辞的,无法以目前的形式得到合理的回答。如需帮助澄清这个问题以便重新打开它,visit the help center .
我的公司只使用 MS Office 2003 产品,所以我必须坚持下去。由于我的工作性质,我需要使用很多“复制和粘贴”功能。源数据主要来自网站,我将数据粘贴到 Excel 中的单元格中。问题是剪贴板保
我是一名优秀的程序员,十分优秀!