方案中的异或

发布于 2024-08-15 14:04:52 字数 97 浏览 3 评论 0原文

方案中有什么独特或功能?我尝试过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

沒落の蓅哖 2024-08-22 14:04:52

如果 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 as eq?

朕就是辣么酷 2024-08-22 14:04:52

据我所知,从R6RS(方案的最新定义)来看,没有预先定义的排他性- 或操作。但是,对于布尔值,xor 相当于 not equals,因此如果没有内置函数,您自己定义它确实非常容易。

假设参数仅限于方案布尔值 #f#t

(define (xor a b)
  (not (boolean=? a b)))

就可以完成这项工作。

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 to not 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,

(define (xor a b)
  (not (boolean=? a b)))

will do the job.

小嗷兮 2024-08-22 14:04:52

如果您的意思是两个整数的按位异或,那么每个方案都有自己的名称(如果有),因为它不符合任何标准。例如,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...)

你的呼吸 2024-08-22 14:04:52

有点不同风格的答案:

(define xor
  (lambda (a b)
    (cond
      (a (not b))
      (else b))))   

Kind of a different style of answer:

(define xor
  (lambda (a b)
    (cond
      (a (not b))
      (else b))))   
马蹄踏│碎落叶 2024-08-22 14:04:52

阅读 SRFI-1 让我对答案有了新的认识。忘记效率和简单性问题,甚至忘记测试!这种美丽可以满足一切:

(define (xor . args)
  (odd? (count (lambda (x) (eqv? x #t)) args)))

或者如果您愿意:

(define (true? x) (eqv? x #t))
(define (xor . args) (odd? (count true? args)))

Reading SRFI-1 shed a new light upon my answer. Forget efficiency and simplicity concerns or even testing! This beauty does it all:

(define (xor . args)
  (odd? (count (lambda (x) (eqv? x #t)) args)))

Or if you prefer:

(define (true? x) (eqv? x #t))
(define (xor . args) (odd? (count true? args)))
迷鸟归林 2024-08-22 14:04:52
(define (xor a b)
  (and 
   (not (and a b))
   (or a b)))
(define (xor a b)
  (and 
   (not (and a b))
   (or a b)))
梦途 2024-08-22 14:04:52

由于异或可以与任意数量的参数一起使用,因此唯一的要求是真实出现的数量是奇数。它可以大致这样定义:

(define (true? x) (eqv? x #t))

(define (xor . args)
  (odd? (length (filter true? args))))

不需要进行参数检查,因为任何数量的参数(包括没有)都会返回正确的答案。

然而,这种简单的实现存在效率问题:length和filter都遍历列表两次;所以我想我可以删除两者以及另一个无用的谓词过程“true?”。

值奇数?当 args 没有剩余的真实计算成员时,receive 是累加器(又名 acc)的值。如果存在计算 true 的成员,则重复使用 acc+1 和从下一个 true 值开始的其余参数,或者计算为 false,这将导致 acc 与最后的计数一起返回。

(define (xor . args)
  (odd? (let count ([args (memv #t args)]
                    [acc 0])
          (if args
              (count (memv #t (cdr args))
                     (+ acc 1))
              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:

(define (true? x) (eqv? x #t))

(define (xor . args)
  (odd? (length (filter true? args))))

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.

(define (xor . args)
  (odd? (let count ([args (memv #t args)]
                    [acc 0])
          (if args
              (count (memv #t (cdr args))
                     (+ acc 1))
              acc))))
呆头 2024-08-22 14:04:52
> (define (xor a b)(not (equal? (and a #t)(and b #t))))
> (xor 'hello 'world)
$9 = #f
> (xor #f #f)
$10 = #f
> (xor (> 1 100)(< 1 100))
$11 = #t
> (define (xor a b)(not (equal? (and a #t)(and b #t))))
> (xor 'hello 'world)
$9 = #f
> (xor #f #f)
$10 = #f
> (xor (> 1 100)(< 1 100))
$11 = #t
伪心 2024-08-22 14:04:52

我最近修改了我的代码,因为我需要在方案中进行“异或”,但发现它不够好......

首先,我之前对“true?”的定义假设参数已在布尔运算下进行了测试。所以我将:

(define (true? x) (eqv? #t))

... 更改为:

(define (true? x) (not (eqv? x #f)))

... 这更像是“真实”的“真实”定义?然而,由于“xor”返回#t,如果它的参数有“奇数”? “true”参数的数量,测试偶数个 false 情况是等效的。这是我修改后的“异或”:

(define (xor . args)
  (even? (count (lambda (x) (eqv? x #f)) args)))

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:

(define (true? x) (eqv? #t))

... for:

(define (true? x) (not (eqv? x #f)))

... 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:

(define (xor . args)
  (even? (count (lambda (x) (eqv? x #f)) args)))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文