Equal Operator in Common Lisp

Why is this:

(every (lambda (x) (equal "a" x)) "aaaaa") 

and this:

 (every (lambda (x) (equal "a" x)) "a") 

return NIL , and this:

 (every (lambda (x) (equal "a" x)) '("a" "a" "a" "a")) 

returns T ? I thought every worked on all sequences.

+4
source share
2 answers

You can always find it yourself. The test is only a few seconds if you use the interactive Lisp system:

 CL-USER 1 > (every (lambda (x) (equal "a" x)) "a") NIL 

Above returns NIL.

Now use the Common Lisp DESCRIBE function to get the described data.

 CL-USER 2 > (every (lambda (x) (describe x) (describe "a") (equal "a" x)) "a") #\a is a CHARACTER Name "Latin-Small-Letter-A" Code 97 Bits 0 Font 0 Function-Key-P NIL 

So the value of x is a symbol. The character #\a .

 "a" is a SIMPLE-BASE-STRING 0 #\a NIL 

Type "a" is SIMPLE-BASE-STRING (here in LispWorks).

If you look at the definition of EQUAL , you will see that the character and string are never equal, because they are of different types.

 CL-USER 3 > (equal #\a "a") NIL 
+15
source

Because in case 1 and 2 you are comparing "a" and #\a , but in the latter case you are comparing "a" and "a" . Line items are characters, not other lines.

For instance:

 (every (lambda (x) (equal #\ax)) "aaaaa") => T 

Another alternative is forcing x to a string:

 (every (lambda (x) (equal "a" (string x))) "aaaaa") 
+13
source

Source: https://habr.com/ru/post/1489390/


All Articles