2009-02-13 124 views

回答

66

Common Lisp: Equality Predicates

(eq x y)當且僅當xy是相同的同一對象是真實的。

如果eql謂詞的參數是eq,或者它們是具有相同值的相同類型的數字,或者它們是表示相同字符的字符對象,則謂詞爲true。

如果equal的參數的結構類似(同構)對象,則該謂詞爲真。一個粗略的經驗法則是,兩個對象是相等的,當且僅當它們的打印表示是相同的。

兩個對象如果相等,則爲equalp;如果它們是字符並且滿足char-equal,忽略字母大小寫和字符的某些其他屬性;如果它們是數字並且具有相同的數值,即使它們具有不同的類型;或者它們的組件全部是equalp

這裏有一些例子來自同一頁我連接到上面:

(eq 'a 'b) is false. 
(eq 'a 'a) is true. 
(eq 3 3) might be true or false, depending on the implementation. 
(eq 3 3.0) is false. 
(eq 3.0 3.0) might be true or false, depending on the implementation. 
(eq #c(3 -4) #c(3 -4)) 
    might be true or false, depending on the implementation. 
(eq #c(3 -4.0) #c(3 -4)) is false. 
(eq (cons 'a 'b) (cons 'a 'c)) is false. 
(eq (cons 'a 'b) (cons 'a 'b)) is false. 
(eq '(a . b) '(a . b)) might be true or false. 
(progn (setq x (cons 'a 'b)) (eq x x)) is true. 
(progn (setq x '(a . b)) (eq x x)) is true. 
(eq #\A #\A) might be true or false, depending on the implementation. 
(eq "Foo" "Foo") might be true or false. 
(eq "Foo" (copy-seq "Foo")) is false. 
(eq "FOO" "foo") is false. 


(eql 'a 'b) is false. 
(eql 'a 'a) is true. 
(eql 3 3) is true. 
(eql 3 3.0) is false. 
(eql 3.0 3.0) is true. 
(eql #c(3 -4) #c(3 -4)) is true. 
(eql #c(3 -4.0) #c(3 -4)) is false. 
(eql (cons 'a 'b) (cons 'a 'c)) is false. 
(eql (cons 'a 'b) (cons 'a 'b)) is false. 
(eql '(a . b) '(a . b)) might be true or false. 
(progn (setq x (cons 'a 'b)) (eql x x)) is true. 
(progn (setq x '(a . b)) (eql x x)) is true. 
(eql #\A #\A) is true. 
(eql "Foo" "Foo") might be true or false. 
(eql "Foo" (copy-seq "Foo")) is false. 
(eql "FOO" "foo") is false. 


(equal 'a 'b) is false. 
(equal 'a 'a) is true. 
(equal 3 3) is true. 
(equal 3 3.0) is false. 
(equal 3.0 3.0) is true. 
(equal #c(3 -4) #c(3 -4)) is true. 
(equal #c(3 -4.0) #c(3 -4)) is false. 
(equal (cons 'a 'b) (cons 'a 'c)) is false. 
(equal (cons 'a 'b) (cons 'a 'b)) is true. 
(equal '(a . b) '(a . b)) is true. 
(progn (setq x (cons 'a 'b)) (equal x x)) is true. 
(progn (setq x '(a . b)) (equal x x)) is true. 
(equal #\A #\A) is true. 
(equal "Foo" "Foo") is true. 
(equal "Foo" (copy-seq "Foo")) is true. 
(equal "FOO" "foo") is false. 


(equalp 'a 'b) is false. 
(equalp 'a 'a) is true. 
(equalp 3 3) is true. 
(equalp 3 3.0) is true. 
(equalp 3.0 3.0) is true. 
(equalp #c(3 -4) #c(3 -4)) is true. 
(equalp #c(3 -4.0) #c(3 -4)) is true. 
(equalp (cons 'a 'b) (cons 'a 'c)) is false. 
(equalp (cons 'a 'b) (cons 'a 'b)) is true. 
(equalp '(a . b) '(a . b)) is true. 
(progn (setq x (cons 'a 'b)) (equalp x x)) is true. 
(progn (setq x '(a . b)) (equalp x x)) is true. 
(equalp #\A #\A) is true. 
(equalp "Foo" "Foo") is true. 
(equalp "Foo" (copy-seq "Foo")) is true. 
(equalp "FOO" "foo") is true. 
25

一些更多的注意事項:

  • 時沒有指定測試

  • 大多數CL功能隱含使用EQL

  • 另請參見STRING-EQUAL和= TREE-EQUAL

  • 在EQ的核心通常是一個指針比較

而且一個粗略的指南:

 
To compare against...  Use... 

Objects/Structs   EQ 

NIL      EQ (but the function NULL is more concise and probably cheaper) 

T       EQ (or just the value but then you don't care for the type) 

Precise numbers   EQL 

Floats      = 

Characters     EQL or CHAR-EQUAL 

Lists, Conses, Sequences EQ (if you want the exact same object) 
          EQUAL (if you just care about elements) 

Strings     EQUAL (case-sensitive), EQUALP (case-insensitive) 
          STRING-EQUAL (if you throw symbols into the mix) 

Trees (lists of lists)  TREE-EQUAL (with appropriate :TEST argument) 

注意,對於效率通常EQ >> EQL >> EQUAL >> EQUALP。

11

here和我的老師滑梯

EQ測試,看看它的參數(由計算機內存的同一塊表示)是相同的符號或沒有。

例如:

(當量 'A' B)NIL
(當量 'RAM' RAM)筆
(當量(利弊 'A 'B)(利弊一個' B')) ; 這是因爲不同的呼叫兩個缺點做,使他們顯然會被分配不同的內存塊

EQL第一次測試,看看它的參數滿足EQ,如果不是,它會嘗試看看他們是否 是相同類型和值的數字。

例如:

(EQL 4 4.0)NIL
(EQL 4 4)T

現在請注意一個

(當量4.0 4.0)NIL;依如第一個(接受的)答案
(eql 4.0 4.0)中所述的平臺上;參數的類型和值是相同的

在某些實現(例如4.0 4.0)上可能會返回true,因爲它沒有在標準中指定實現是否應該只在內存中保留一個數字和字符的副本,就像它在符號中一樣)。根據經驗不要在數字和字符上使用eq,除非你真的知道你在做什麼。

等於是「理智的」比較函數。作爲一個經驗法則,你可以把它看作告訴你兩個物體看起來是否相同(結構相似或同構)。這可能是您想要用於普遍平等的運營商。它的行爲就像EQL數字,字符和符號,但對於列表(conses之外)和字符串它告訴如果它們的元素

例如:

(等於4 4)T
(等於(+ 2 2)4)T

現在請注意一個

(EQL(利弊 'A' b)(利弊 'A' b))的NIL
(等於(利弊 'A' b)(利弊'a'b))T; 等於對的事情,打印相同

equalp就像是平等的,只是更先進通常爲true。數字的比較是類型不敏感的。字符和字符串的比較不區分大小寫。

例如:

(equalp(利弊 'A' B)(利弊 'A' B))筆; 相同等於

現在請注意一個

等於(4 4.0)NIL
equalp(4 4.0)筆; 由於equalp對待數字類型不敏感

相關問題