programing tip

Common Lisp에서 eq, eql, equal 및 equalp의 차이점은 무엇입니까?

itbloger 2020. 11. 5. 07:46
반응형

Common Lisp에서 eq, eql, equal 및 equalp의 차이점은 무엇입니까?


무엇의 차이입니다 eq, eql, equalequalp커먼 리스프에? 나는 그들 중 일부는 유형을 확인하고 일부는 유형을 모두 확인하지만 어떤 것이 무엇입니까? 하나가 다른 것보다 언제 더 잘 사용됩니까?


에서 커먼 리스프 : 평등 술어

(eq x y)경우에만, 사실 xy같은 동일한 개체입니다.

eql술어는 인수가있는 경우 사실 eq, 또는 그들은 같은 값으로 동일한 유형의 숫자, 또는 경우에 그들은 같은 문자를 나타내는 문자 객체를 경우.

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.

추가 참고 사항 :

  • 테스트가 지정되지 않은 경우 대부분의 CL 함수는 암시 적으로 EQL을 사용합니다.

  • STRING-EQUAL, = 및 TREE-EQUAL 참조

  • EQ의 핵심은 일반적으로 포인터 비교입니다.

그리고 대략적인 가이드 :

비교하려면 ... 사용 ...

개체 / 구조 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)

Note that for efficiency usually EQ >> EQL >> EQUAL >> EQUALP.


From here and my teacher's slides

eq tests to see if its arguments(represented by the same chunk of computer memory) are same symbol or not.

For Example:

(eq ‘A ‘B) NIL
(eq ‘RAM ‘RAM) T
(eq (cons 'a 'b) (cons a' b')) ; This is because different calls are made for both cons so they will obviously be allocated different memory chunks

eql first tests to see if its arguments satisfy EQ, if not, it tries to see if they are numbers of the same type and values.

For Example:

(eql 4 4.0) NIL
(eql 4 4) T

Now note a difference:

(eq 4.0 4.0) NIL ;Depend upon platform as described in first (accepted)answer
(eql 4.0 4.0) T ;type and value of arguments is same

On some implementations (eq 4.0 4.0) may return true because it is not specified in the standard whether an implementation should keep just one copy of numbers and chars in memory, like it does with symbols).As a rule of thumb don’t use eq on numbers and characters, unless you really know what you’re doing.

equal is a “saner” comparison function. As a rule of thumb, you can think of it as telling you whether two objects look the same (structurally similar, or isomorphic). It is probably the operator you want to use for general equality. It behaves like eql for numbers, characters and symbols, but for lists (conses) and strings it tells if their elements

For Example:

(equal 4 4) T
(equal (+ 2 2) 4) T

Now note a difference

(eql (cons 'a 'b) (cons 'a 'b)) NIL
(equal (cons 'a 'b) (cons 'a 'b)) T ; equal is usually true for things that print the same

equalp is like equal, just more advanced. Comparison of numbers is type insensitive. Comparison of chars and strings is case insensitive.

For Example:

(equalp (cons 'a 'b) (cons 'a 'b)) T ;same as equal

Now note a difference

equal(4 4.0) NIL
equalp(4 4.0) T ; As equalp treats numbers type insensitively

참고URL : https://stackoverflow.com/questions/547436/whats-the-difference-between-eq-eql-equal-and-equalp-in-common-lisp

반응형