9a1f4d090e9281422cfac3103f4c093040f595a9
1 /* Copyright (C) 1995, 96, 97, 98, 99, 2000, 2001 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include "libguile/_scm.h"
21 #include "libguile/eq.h"
22 #include "libguile/list.h"
23 #include "libguile/lang.h"
25 #include "libguile/validate.h"
26 #include "libguile/alist.h"
30 SCM_DEFINE (scm_acons
, "acons", 3, 0, 0,
31 (SCM key
, SCM value
, SCM alist
),
32 "Add a new key-value pair to @var{alist}. A new pair is\n"
33 "created whose car is @var{key} and whose cdr is @var{value}, and the\n"
34 "pair is consed onto @var{alist}, and the new list is returned. This\n"
35 "function is @emph{not} destructive; @var{alist} is not modified.")
36 #define FUNC_NAME s_scm_acons
38 return scm_cell (SCM_UNPACK (scm_cell (SCM_UNPACK (key
),
46 SCM_DEFINE (scm_sloppy_assq
, "sloppy-assq", 2, 0, 0,
48 "Behaves like @code{assq} but does not do any error checking.\n"
49 "Recommended only for use in Guile internals.")
50 #define FUNC_NAME s_scm_sloppy_assq
52 for (; SCM_CONSP (alist
); alist
= SCM_CDR (alist
))
54 SCM tmp
= SCM_CAR (alist
);
55 if (SCM_CONSP (tmp
) && scm_is_eq (SCM_CAR (tmp
), key
))
64 SCM_DEFINE (scm_sloppy_assv
, "sloppy-assv", 2, 0, 0,
66 "Behaves like @code{assv} but does not do any error checking.\n"
67 "Recommended only for use in Guile internals.")
68 #define FUNC_NAME s_scm_sloppy_assv
70 for (; SCM_CONSP (alist
); alist
= SCM_CDR (alist
))
72 SCM tmp
= SCM_CAR (alist
);
74 && scm_is_true (scm_eqv_p (SCM_CAR (tmp
), key
)))
82 SCM_DEFINE (scm_sloppy_assoc
, "sloppy-assoc", 2, 0, 0,
84 "Behaves like @code{assoc} but does not do any error checking.\n"
85 "Recommended only for use in Guile internals.")
86 #define FUNC_NAME s_scm_sloppy_assoc
88 for (; SCM_CONSP (alist
); alist
= SCM_CDR (alist
))
90 SCM tmp
= SCM_CAR (alist
);
92 && scm_is_true (scm_equal_p (SCM_CAR (tmp
), key
)))
102 SCM_DEFINE (scm_assq
, "assq", 2, 0, 0,
103 (SCM key
, SCM alist
),
104 "@deffnx {Scheme Procedure} assv key alist\n"
105 "@deffnx {Scheme Procedure} assoc key alist\n"
106 "Fetch the entry in @var{alist} that is associated with @var{key}. To\n"
107 "decide whether the argument @var{key} matches a particular entry in\n"
108 "@var{alist}, @code{assq} compares keys with @code{eq?}, @code{assv}\n"
109 "uses @code{eqv?} and @code{assoc} uses @code{equal?}. If @var{key}\n"
110 "cannot be found in @var{alist} (according to whichever equality\n"
111 "predicate is in use), then return @code{#f}. These functions\n"
112 "return the entire alist entry found (i.e. both the key and the value).")
113 #define FUNC_NAME s_scm_assq
116 for (; SCM_CONSP (ls
); ls
= SCM_CDR (ls
))
118 SCM tmp
= SCM_CAR (ls
);
119 SCM_ASSERT_TYPE (SCM_CONSP (tmp
), alist
, SCM_ARG2
, FUNC_NAME
,
121 if (scm_is_eq (SCM_CAR (tmp
), key
))
124 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (ls
), alist
, SCM_ARG2
, FUNC_NAME
,
131 SCM_DEFINE (scm_assv
, "assv", 2, 0, 0,
132 (SCM key
, SCM alist
),
133 "Behaves like @code{assq} but uses @code{eqv?} for key comparison.")
134 #define FUNC_NAME s_scm_assv
137 for(; SCM_CONSP (ls
); ls
= SCM_CDR (ls
))
139 SCM tmp
= SCM_CAR (ls
);
140 SCM_ASSERT_TYPE (SCM_CONSP (tmp
), alist
, SCM_ARG2
, FUNC_NAME
,
142 if (scm_is_true (scm_eqv_p (SCM_CAR (tmp
), key
)))
145 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (ls
), alist
, SCM_ARG2
, FUNC_NAME
,
152 SCM_DEFINE (scm_assoc
, "assoc", 2, 0, 0,
153 (SCM key
, SCM alist
),
154 "Behaves like @code{assq} but uses @code{equal?} for key comparison.")
155 #define FUNC_NAME s_scm_assoc
158 for(; SCM_CONSP (ls
); ls
= SCM_CDR (ls
))
160 SCM tmp
= SCM_CAR (ls
);
161 SCM_ASSERT_TYPE (SCM_CONSP (tmp
), alist
, SCM_ARG2
, FUNC_NAME
,
163 if (scm_is_true (scm_equal_p (SCM_CAR (tmp
), key
)))
166 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (ls
), alist
, SCM_ARG2
, FUNC_NAME
,
175 /* Dirk:API2.0:: We should not return #f if the key was not found. In the
176 * current solution we can not distinguish between finding a (key . #f) pair
177 * and not finding the key at all.
179 * Possible alternative solutions:
180 * 1) Remove assq-ref from the API: assq is sufficient.
181 * 2) Signal an error (what error type?) if the key is not found.
182 * 3) provide an additional 'default' parameter.
183 * 3.1) The default parameter is mandatory.
184 * 3.2) The default parameter is optional, but if no default is given and
185 * the key is not found, signal an error (what error type?).
187 SCM_DEFINE (scm_assq_ref
, "assq-ref", 2, 0, 0,
188 (SCM alist
, SCM key
),
189 "@deffnx {Scheme Procedure} assv-ref alist key\n"
190 "@deffnx {Scheme Procedure} assoc-ref alist key\n"
191 "Like @code{assq}, @code{assv} and @code{assoc}, except that only the\n"
192 "value associated with @var{key} in @var{alist} is returned. These\n"
193 "functions are equivalent to\n\n"
195 "(let ((ent (@var{associator} @var{key} @var{alist})))\n"
196 " (and ent (cdr ent)))\n"
198 "where @var{associator} is one of @code{assq}, @code{assv} or @code{assoc}.")
199 #define FUNC_NAME s_scm_assq_ref
203 handle
= scm_sloppy_assq (key
, alist
);
204 if (SCM_CONSP (handle
))
206 return SCM_CDR (handle
);
213 SCM_DEFINE (scm_assv_ref
, "assv-ref", 2, 0, 0,
214 (SCM alist
, SCM key
),
215 "Behaves like @code{assq-ref} but uses @code{eqv?} for key comparison.")
216 #define FUNC_NAME s_scm_assv_ref
220 handle
= scm_sloppy_assv (key
, alist
);
221 if (SCM_CONSP (handle
))
223 return SCM_CDR (handle
);
230 SCM_DEFINE (scm_assoc_ref
, "assoc-ref", 2, 0, 0,
231 (SCM alist
, SCM key
),
232 "Behaves like @code{assq-ref} but uses @code{equal?} for key comparison.")
233 #define FUNC_NAME s_scm_assoc_ref
237 handle
= scm_sloppy_assoc (key
, alist
);
238 if (SCM_CONSP (handle
))
240 return SCM_CDR (handle
);
251 SCM_DEFINE (scm_assq_set_x
, "assq-set!", 3, 0, 0,
252 (SCM alist
, SCM key
, SCM val
),
253 "@deffnx {Scheme Procedure} assv-set! alist key value\n"
254 "@deffnx {Scheme Procedure} assoc-set! alist key value\n"
255 "Reassociate @var{key} in @var{alist} with @var{value}: find any existing\n"
256 "@var{alist} entry for @var{key} and associate it with the new\n"
257 "@var{value}. If @var{alist} does not contain an entry for @var{key},\n"
258 "add a new one. Return the (possibly new) alist.\n\n"
259 "These functions do not attempt to verify the structure of @var{alist},\n"
260 "and so may cause unusual results if passed an object that is not an\n"
262 #define FUNC_NAME s_scm_assq_set_x
266 handle
= scm_sloppy_assq (key
, alist
);
267 if (SCM_CONSP (handle
))
269 SCM_SETCDR (handle
, val
);
273 return scm_acons (key
, val
, alist
);
277 SCM_DEFINE (scm_assv_set_x
, "assv-set!", 3, 0, 0,
278 (SCM alist
, SCM key
, SCM val
),
279 "Behaves like @code{assq-set!} but uses @code{eqv?} for key comparison.")
280 #define FUNC_NAME s_scm_assv_set_x
284 handle
= scm_sloppy_assv (key
, alist
);
285 if (SCM_CONSP (handle
))
287 SCM_SETCDR (handle
, val
);
291 return scm_acons (key
, val
, alist
);
295 SCM_DEFINE (scm_assoc_set_x
, "assoc-set!", 3, 0, 0,
296 (SCM alist
, SCM key
, SCM val
),
297 "Behaves like @code{assq-set!} but uses @code{equal?} for key comparison.")
298 #define FUNC_NAME s_scm_assoc_set_x
302 handle
= scm_sloppy_assoc (key
, alist
);
303 if (SCM_CONSP (handle
))
305 SCM_SETCDR (handle
, val
);
309 return scm_acons (key
, val
, alist
);
316 SCM_DEFINE (scm_assq_remove_x
, "assq-remove!", 2, 0, 0,
317 (SCM alist
, SCM key
),
318 "@deffnx {Scheme Procedure} assv-remove! alist key\n"
319 "@deffnx {Scheme Procedure} assoc-remove! alist key\n"
320 "Delete the first entry in @var{alist} associated with @var{key}, and return\n"
321 "the resulting alist.")
322 #define FUNC_NAME s_scm_assq_remove_x
326 handle
= scm_sloppy_assq (key
, alist
);
327 if (SCM_CONSP (handle
))
328 alist
= scm_delq1_x (handle
, alist
);
335 SCM_DEFINE (scm_assv_remove_x
, "assv-remove!", 2, 0, 0,
336 (SCM alist
, SCM key
),
337 "Behaves like @code{assq-remove!} but uses @code{eqv?} for key comparison.")
338 #define FUNC_NAME s_scm_assv_remove_x
342 handle
= scm_sloppy_assv (key
, alist
);
343 if (SCM_CONSP (handle
))
344 alist
= scm_delq1_x (handle
, alist
);
351 SCM_DEFINE (scm_assoc_remove_x
, "assoc-remove!", 2, 0, 0,
352 (SCM alist
, SCM key
),
353 "Behaves like @code{assq-remove!} but uses @code{equal?} for key comparison.")
354 #define FUNC_NAME s_scm_assoc_remove_x
358 handle
= scm_sloppy_assoc (key
, alist
);
359 if (SCM_CONSP (handle
))
360 alist
= scm_delq1_x (handle
, alist
);
374 #include "libguile/alist.x"