方案中的异或
方案中有什么独特或功能?我尝试过 xor
和 ^
,但两者都给我一个未绑定的局部变量错误。
谷歌搜索什么也没找到。
What is the exclusive or functions in scheme? I've tried xor
and ^
, but both give me an unbound local variable error.
Googling found nothing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
如果 not equals 有效,我建议您使用
(not (equal? foo bar))
。请注意,可能有适合您情况的更快比较器,例如eq?
I suggest you use
(not (equal? foo bar))
if not equals works. Please note that there may be faster comparators for your situiation such aseq?
据我所知,从R6RS(方案的最新定义)来看,没有预先定义的排他性- 或操作。但是,对于布尔值,
xor
相当于not equals
,因此如果没有内置函数,您自己定义它确实非常容易。假设参数仅限于方案布尔值
#f
和#t
,就可以完成这项工作。
As far as I can tell from the R6RS (the latest definition of scheme), there is no pre-defined exclusive-or operation. However,
xor
is equivalent tonot equals
for boolean values so it's really quite easy to define on your own if there isn't a builtin function for it.Assuming the arguments are restricted to the scheme booleans values
#f
and#t
,will do the job.
如果您的意思是两个整数的按位异或,那么每个方案都有自己的名称(如果有),因为它不符合任何标准。例如,PLT 有这些按位函数< /a>,包括
按位异或
。(呃,如果你谈论布尔值,那么是的,
not
&or
是......)If you mean bitwise xor of two integers, then each Scheme has it's own name (if any) since it's not in any standard. For example, PLT has these bitwise functions, including
bitwise-xor
.(Uh, if you talk about booleans, then yes,
not
&or
are it...)有点不同风格的答案:
Kind of a different style of answer:
阅读 SRFI-1 让我对答案有了新的认识。忘记效率和简单性问题,甚至忘记测试!这种美丽可以满足一切:
或者如果您愿意:
Reading SRFI-1 shed a new light upon my answer. Forget efficiency and simplicity concerns or even testing! This beauty does it all:
Or if you prefer:
由于异或可以与任意数量的参数一起使用,因此唯一的要求是真实出现的数量是奇数。它可以大致这样定义:
不需要进行参数检查,因为任何数量的参数(包括没有)都会返回正确的答案。
然而,这种简单的实现存在效率问题:length和filter都遍历列表两次;所以我想我可以删除两者以及另一个无用的谓词过程“true?”。
值奇数?当 args 没有剩余的真实计算成员时,receive 是累加器(又名 acc)的值。如果存在计算 true 的成员,则重复使用 acc+1 和从下一个 true 值开始的其余参数,或者计算为 false,这将导致 acc 与最后的计数一起返回。
Since xor could be used with any number of arguments, the only requirement is that the number of true occurences be odd. It could be defined roughly this way:
No argument checking needs to be done since any number of arguments (including none) will return the right answer.
However, this simple implementation has efficiency problems: both length and filter traverse the list twice; so I thought I could remove both and also the other useless predicate procedure "true?".
The value odd? receives is the value of the accumulator (aka acc) when args has no remaining true-evaluating members. If true-evaluating members exist, repeat with acc+1 and the rest of the args starting at the next true value or evaluate to false, which will cause acc to be returned with the last count.
我最近修改了我的代码,因为我需要在方案中进行“异或”,但发现它不够好......
首先,我之前对“true?”的定义假设参数已在布尔运算下进行了测试。所以我将:
... 更改为:
... 这更像是“真实”的“真实”定义?然而,由于“xor”返回#t,如果它的参数有“奇数”? “true”参数的数量,测试偶数个 false 情况是等效的。这是我修改后的“异或”:
I revised my code recently because I needed 'xor in scheme and found out it wasn't good enough...
First, my earlier definition of 'true? made the assumption that arguments had been tested under a boolean operation. So I change:
... for:
... which is more like the "true" definition of 'true? However, since 'xor returns #t if its arguments have an 'odd? number of "true" arguments, testing for an even number of false cases is equivalent. So here's my revised 'xor: