2014-03-31 36 views
-2
(define xor 
    (lambda (x y) 
    (if (boolean=? x y) 
     #f 
     #t))) 

它是如何工作的? 我不明白,異或在球拍,我不明白它是如何工作的

+0

嗯......因爲我從來沒有見過球拍代碼之前(看起來像LISP),並給予XOR ='X &&的傳統定義ÿ||! !x && y' ...那麼它看起來好像會起作用(因爲我會假設'boolean =?'會檢查兩者是否爲真 - 我可能會犯這個錯誤)。 – Makoto

+0

你不幫我。 – user3466525

+0

您可能希望閱讀[SO問題清單](http://meta.stackexchange.com/questions/156810/stack-overflow-question-checklist)以改善您的問題。例如,在不知道你嘗試過的東西的情況下回答你的問題是很困難的。 –

回答

4

讓我們來看看xortruth table

x xor y 
T F T    ; x and y are both true, x xor y is false 
T T F    ; x and y are different, x xor y is true 
F T T    ; x and y are different, x xor y is true 
F F F    ; x and y are both false, x xor y is false 

換句話說:如果兩個xy相等,則xor是假的。但如果xy不同,那麼xor爲真。而這也正是這個計劃的代碼這樣說:

(if (boolean=? x y) ; are x and y equal? 
    #f    ; then xor is false 
    #t)    ; otherwise xor is true 
相關問題