1 /* Copyright (C) 1995,1996,1997,1998,2000,2001,2003, 2004 Free Software Foundation, Inc.
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "libguile/_scm.h"
24 #include "libguile/ramap.h"
25 #include "libguile/stackchk.h"
26 #include "libguile/strorder.h"
27 #include "libguile/async.h"
28 #include "libguile/root.h"
29 #include "libguile/smob.h"
30 #include "libguile/unif.h"
31 #include "libguile/vectors.h"
33 #include "libguile/validate.h"
34 #include "libguile/eq.h"
42 SCM_DEFINE1 (scm_eq_p
, "eq?", scm_tc7_rpsubr
,
44 "Return @code{#t} if @var{x} and @var{y} are the same object,\n"
45 "except for numbers and characters. For example,\n"
48 "(define x (vector 1 2 3))\n"
49 "(define y (vector 1 2 3))\n"
51 "(eq? x x) @result{} #t\n"
52 "(eq? x y) @result{} #f\n"
55 "Numbers and characters are not equal to any other object, but\n"
56 "the problem is they're not necessarily @code{eq?} to themselves\n"
57 "either. This is even so when the number comes directly from a\n"
61 "(let ((n (+ 2 3)))\n"
62 " (eq? n n)) @result{} *unspecified*\n"
65 "Generally @code{eqv?} should be used when comparing numbers or\n"
66 "characters. @code{=} or @code{char=?} can be used too.\n"
68 "It's worth noting that end-of-list @code{()}, @code{#t},\n"
69 "@code{#f}, a symbol of a given name, and a keyword of a given\n"
70 "name, are unique objects. There's just one of each, so for\n"
71 "instance no matter how @code{()} arises in a program, it's the\n"
72 "same object and can be compared with @code{eq?},\n"
75 "(define x (cdr '(123)))\n"
76 "(define y (cdr '(456)))\n"
77 "(eq? x y) @result{} #t\n"
79 "(define x (string->symbol \"foo\"))\n"
80 "(eq? x 'foo) @result{} #t\n"
82 #define FUNC_NAME s_scm_eq_p
84 return scm_from_bool (scm_is_eq (x
, y
));
88 /* We compare doubles in a special way for 'eqv?' to be able to
89 distinguish plus and minus zero and to identify NaNs.
93 real_eqv (double x
, double y
)
95 return !memcmp (&x
, &y
, sizeof(double)) || (x
!= x
&& y
!= y
);
99 SCM_PRIMITIVE_GENERIC_1 (scm_eqv_p
, "eqv?", scm_tc7_rpsubr
,
101 "Return @code{#t} if @var{x} and @var{y} are the same object, or\n"
102 "for characters and numbers the same value.\n"
104 "On objects except characters and numbers, @code{eqv?} is the\n"
105 "same as @code{eq?}, it's true if @var{x} and @var{y} are the\n"
108 "If @var{x} and @var{y} are numbers or characters, @code{eqv?}\n"
109 "compares their type and value. An exact number is not\n"
110 "@code{eqv?} to an inexact number (even if their value is the\n"
114 "(eqv? 3 (+ 1 2)) @result{} #t\n"
115 "(eqv? 1 1.0) @result{} #f\n"
117 #define FUNC_NAME s_scm_eqv_p
119 if (scm_is_eq (x
, y
))
125 /* this ensures that types and scm_length are the same. */
127 if (SCM_CELL_TYPE (x
) != SCM_CELL_TYPE (y
))
129 /* fractions use 0x10000 as a flag (at the suggestion of Marius Vollmer),
130 but this checks the entire type word, so fractions may be accidentally
131 flagged here as unequal. Perhaps I should use the 4th double_cell word?
134 /* treat mixes of real and complex types specially */
135 if (SCM_INEXACTP (x
))
138 return scm_from_bool (SCM_COMPLEXP (y
)
139 && real_eqv (SCM_REAL_VALUE (x
),
140 SCM_COMPLEX_REAL (y
))
141 && SCM_COMPLEX_IMAG (y
) == 0.0);
143 return scm_from_bool (SCM_REALP (y
)
144 && real_eqv (SCM_COMPLEX_REAL (x
),
146 && SCM_COMPLEX_IMAG (x
) == 0.0);
149 if (SCM_FRACTIONP (x
) && SCM_FRACTIONP (y
))
150 return scm_i_fraction_equalp (x
, y
);
156 return scm_from_bool (scm_i_bigcmp (x
, y
) == 0);
157 } else if (SCM_REALP (x
)) {
158 return scm_from_bool (real_eqv (SCM_REAL_VALUE (x
), SCM_REAL_VALUE (y
)));
159 } else if (SCM_FRACTIONP (x
)) {
160 return scm_i_fraction_equalp (x
, y
);
161 } else { /* complex */
162 return scm_from_bool (real_eqv (SCM_COMPLEX_REAL (x
),
163 SCM_COMPLEX_REAL (y
))
164 && real_eqv (SCM_COMPLEX_IMAG (x
),
165 SCM_COMPLEX_IMAG (y
)));
168 if (SCM_UNPACK (g_scm_eqv_p
))
169 return scm_call_generic_2 (g_scm_eqv_p
, x
, y
);
176 SCM_PRIMITIVE_GENERIC_1 (scm_equal_p
, "equal?", scm_tc7_rpsubr
,
178 "Return @code{#t} if @var{x} and @var{y} are the same type, and\n"
179 "their contents or value are equal.\n"
181 "For a pair, string, vector or array, @code{equal?} compares the\n"
182 "contents, and does so using using the same @code{equal?}\n"
183 "recursively, so a deep structure can be traversed.\n"
186 "(equal? (list 1 2 3) (list 1 2 3)) @result{} #t\n"
187 "(equal? (list 1 2 3) (vector 1 2 3)) @result{} #f\n"
190 "For other objects, @code{equal?} compares as per @code{eqv?},\n"
191 "which means characters and numbers are compared by type and\n"
192 "value (and like @code{eqv?}, exact and inexact numbers are not\n"
193 "@code{equal?}, even if their value is the same).\n"
196 "(equal? 3 (+ 1 2)) @result{} #t\n"
197 "(equal? 1 1.0) @result{} #f\n"
200 "Hash tables are currently only compared as per @code{eq?}, so\n"
201 "two different tables are not @code{equal?}, even if their\n"
202 "contents are the same.\n"
204 "@code{equal?} does not support circular data structures, it may\n"
205 "go into an infinite loop if asked to compare two circular lists\n"
208 "New application-defined object types (Smobs) have an\n"
209 "@code{equalp} handler which is called by @code{equal?}. This\n"
210 "lets an application traverse the contents or control what is\n"
211 "considered @code{equal?} for two such objects. If there's no\n"
212 "handler, the default is to just compare as per @code{eq?}.")
213 #define FUNC_NAME s_scm_equal_p
218 if (scm_is_eq (x
, y
))
224 if (scm_is_pair (x
) && scm_is_pair (y
))
226 if (scm_is_false (scm_equal_p (SCM_CAR (x
), SCM_CAR (y
))))
232 if (SCM_TYP7 (x
) == scm_tc7_string
&& SCM_TYP7 (y
) == scm_tc7_string
)
233 return scm_string_equal_p (x
, y
);
234 if (SCM_TYP7 (x
) == scm_tc7_smob
&& SCM_TYP16 (x
) == SCM_TYP16 (y
))
236 int i
= SCM_SMOBNUM (x
);
237 if (!(i
< scm_numsmob
))
239 if (scm_smobs
[i
].equalp
)
240 return (scm_smobs
[i
].equalp
) (x
, y
);
244 /* This ensures that types and scm_length are the same. */
245 if (SCM_CELL_TYPE (x
) != SCM_CELL_TYPE (y
))
247 /* treat mixes of real and complex types specially */
248 if (SCM_INEXACTP (x
) && SCM_INEXACTP (y
))
251 return scm_from_bool (SCM_COMPLEXP (y
)
252 && SCM_REAL_VALUE (x
) == SCM_COMPLEX_REAL (y
)
253 && SCM_COMPLEX_IMAG (y
) == 0.0);
255 return scm_from_bool (SCM_REALP (y
)
256 && SCM_COMPLEX_REAL (x
) == SCM_REAL_VALUE (y
)
257 && SCM_COMPLEX_IMAG (x
) == 0.0);
262 switch (SCM_TYP7 (x
))
270 return scm_bigequal (x
, y
);
272 return scm_real_equalp (x
, y
);
273 case scm_tc16_complex
:
274 return scm_complex_equalp (x
, y
);
275 case scm_tc16_fraction
:
276 return scm_i_fraction_equalp (x
, y
);
280 return scm_i_vector_equal_p (x
, y
);
283 if (SCM_UNPACK (g_scm_equal_p
))
284 return scm_call_generic_2 (g_scm_equal_p
, x
, y
);
298 #include "libguile/eq.x"