I. Comparison of objects
1, eq?
This function is used to compare the addresses of 2 objects, and returns #t if the same is true. In scheme, it is true that #t is used for false #f.
For example, (eq? STR str) returns #t because the address of STR itself is the same, but "scheme" and "scheme" are stored in different addresses, so the function returns #f. Note that you do not use EQ to compare numbers, because there are no specified return values in R5rs and mit-scheme, we recommend using eqv or = instead. Here are some examples:
(Define STR "scheme")
; Value:str
(eq. str str)
; Value: #t
(eq?) "Scheme" "scheme")
; Value: () in R5rs, return #f here
2, Eqv?
The function compares the type and value of 2 objects stored in memory and returns #t if both types and values are consistent. The comparison of a procedure (lambda expression) relies on a specific implementation. This function cannot be used for sequence comparisons similar to tables and strings, because although these sequences appear to be consistent, they are stored in different addresses. The following are also some examples:
(Eqv? 1.0 1.0)
; Value: #t
(eqv? 1 1.0)
; Value: ()
(Eqv?) (List 1 2 3) (List 1 2 3)) Don't compare sequences.
; Value: ()
(Eqv?) "Scheme" "scheme")
; Value: ()
3, equal?
The comparison sequence should be the function.
(equal?) (List 1 2 3) (List 1 2 3))
; Value: #t
(equal?) "Hello" "Hello")
; Value #t
4. Some other functions for comparison
pair? #t
list? If the object is a table, return #t
null? If the object is empty table ' () then return #t .
symbol? If the object is a symbol, return #t .
char? If the object is a character, return #t .
string? If the object is a string, return #t .
number? If the object is a number, return #t .
complex? If the object is a complex number, return .
real? If the object is a real number, return .
rational? If the object is a rational number then return #t .
integer? If the object is an integer, return .
exact? If the object is not a floating-point number, it returns .
Inexact? Returns the #tif the object is a floating-point number.
Odd? returns the #tIf the object is an odd number.
Even? if the object is an even number, the #tis returned.
Postitive? returns the #tif the object is a positive number.
< Span style= "line-height:200%; font-family: ' Microsoft Ya Black ', ' sans-serif '; font-size:12pt ">negative? .
Zero? returns the #tif the object is 0.
Similar to comparing numbers with <=, you can use char=?
,, char<?
char>?
, char<=?
and char>=?
functions when comparing characters.
比较字符串时,可以使用string=?
和string-ci=?
等函数。
[Introduction to Scheme]3 Eqv?, loop, let, Letrec, do, etc. comparison and use