1 /* Copyright (C) 1995, 96, 97, 98, 99, 2000, 2001, 2004, 2006, 2008 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 License
5 * as published by the Free Software Foundation; either version 3 of
6 * the License, or (at your option) any later version.
8 * This library is distributed in the hope that it will be useful, but
9 * 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
25 #include "libguile/_scm.h"
26 #include "libguile/eq.h"
27 #include "libguile/list.h"
28 #include "libguile/lang.h"
30 #include "libguile/validate.h"
31 #include "libguile/pairs.h"
32 #include "libguile/alist.h"
36 SCM_DEFINE (scm_acons
, "acons", 3, 0, 0,
37 (SCM key
, SCM value
, SCM alist
),
38 "Add a new key-value pair to @var{alist}. A new pair is\n"
39 "created whose car is @var{key} and whose cdr is @var{value}, and the\n"
40 "pair is consed onto @var{alist}, and the new list is returned. This\n"
41 "function is @emph{not} destructive; @var{alist} is not modified.")
42 #define FUNC_NAME s_scm_acons
44 return scm_cell (SCM_UNPACK (scm_cell (SCM_UNPACK (key
),
52 SCM_DEFINE (scm_sloppy_assq
, "sloppy-assq", 2, 0, 0,
54 "Behaves like @code{assq} but does not do any error checking.\n"
55 "Recommended only for use in Guile internals.")
56 #define FUNC_NAME s_scm_sloppy_assq
58 for (; scm_is_pair (alist
); alist
= SCM_CDR (alist
))
60 SCM tmp
= SCM_CAR (alist
);
61 if (scm_is_pair (tmp
) && scm_is_eq (SCM_CAR (tmp
), key
))
70 SCM_DEFINE (scm_sloppy_assv
, "sloppy-assv", 2, 0, 0,
72 "Behaves like @code{assv} but does not do any error checking.\n"
73 "Recommended only for use in Guile internals.")
74 #define FUNC_NAME s_scm_sloppy_assv
76 for (; scm_is_pair (alist
); alist
= SCM_CDR (alist
))
78 SCM tmp
= SCM_CAR (alist
);
80 && scm_is_true (scm_eqv_p (SCM_CAR (tmp
), key
)))
88 SCM_DEFINE (scm_sloppy_assoc
, "sloppy-assoc", 2, 0, 0,
90 "Behaves like @code{assoc} but does not do any error checking.\n"
91 "Recommended only for use in Guile internals.")
92 #define FUNC_NAME s_scm_sloppy_assoc
94 for (; scm_is_pair (alist
); alist
= SCM_CDR (alist
))
96 SCM tmp
= SCM_CAR (alist
);
98 && scm_is_true (scm_equal_p (SCM_CAR (tmp
), key
)))
108 SCM_DEFINE (scm_assq
, "assq", 2, 0, 0,
109 (SCM key
, SCM alist
),
110 "@deffnx {Scheme Procedure} assv key alist\n"
111 "@deffnx {Scheme Procedure} assoc key alist\n"
112 "Fetch the entry in @var{alist} that is associated with @var{key}. To\n"
113 "decide whether the argument @var{key} matches a particular entry in\n"
114 "@var{alist}, @code{assq} compares keys with @code{eq?}, @code{assv}\n"
115 "uses @code{eqv?} and @code{assoc} uses @code{equal?}. If @var{key}\n"
116 "cannot be found in @var{alist} (according to whichever equality\n"
117 "predicate is in use), then return @code{#f}. These functions\n"
118 "return the entire alist entry found (i.e. both the key and the value).")
119 #define FUNC_NAME s_scm_assq
122 for(; scm_is_pair (ls
); ls
= SCM_CDR (ls
))
124 SCM tmp
= SCM_CAR (ls
);
125 SCM_ASSERT_TYPE (scm_is_pair (tmp
), alist
, SCM_ARG2
, FUNC_NAME
,
127 if (scm_is_eq (SCM_CAR (tmp
), key
))
130 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (ls
), alist
, SCM_ARG2
, FUNC_NAME
,
137 SCM_DEFINE (scm_assv
, "assv", 2, 0, 0,
138 (SCM key
, SCM alist
),
139 "Behaves like @code{assq} but uses @code{eqv?} for key comparison.")
140 #define FUNC_NAME s_scm_assv
143 for(; scm_is_pair (ls
); ls
= SCM_CDR (ls
))
145 SCM tmp
= SCM_CAR (ls
);
146 SCM_ASSERT_TYPE (scm_is_pair (tmp
), alist
, SCM_ARG2
, FUNC_NAME
,
148 if (scm_is_true (scm_eqv_p (SCM_CAR (tmp
), key
)))
151 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (ls
), alist
, SCM_ARG2
, FUNC_NAME
,
158 SCM_DEFINE (scm_assoc
, "assoc", 2, 0, 0,
159 (SCM key
, SCM alist
),
160 "Behaves like @code{assq} but uses @code{equal?} for key comparison.")
161 #define FUNC_NAME s_scm_assoc
164 for(; scm_is_pair (ls
); ls
= SCM_CDR (ls
))
166 SCM tmp
= SCM_CAR (ls
);
167 SCM_ASSERT_TYPE (scm_is_pair (tmp
), alist
, SCM_ARG2
, FUNC_NAME
,
169 if (scm_is_true (scm_equal_p (SCM_CAR (tmp
), key
)))
172 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (ls
), alist
, SCM_ARG2
, FUNC_NAME
,
181 /* Dirk:API2.0:: We should not return #f if the key was not found. In the
182 * current solution we can not distinguish between finding a (key . #f) pair
183 * and not finding the key at all.
185 * Possible alternative solutions:
186 * 1) Remove assq-ref from the API: assq is sufficient.
187 * 2) Signal an error (what error type?) if the key is not found.
188 * 3) provide an additional 'default' parameter.
189 * 3.1) The default parameter is mandatory.
190 * 3.2) The default parameter is optional, but if no default is given and
191 * the key is not found, signal an error (what error type?).
193 SCM_DEFINE (scm_assq_ref
, "assq-ref", 2, 0, 0,
194 (SCM alist
, SCM key
),
195 "@deffnx {Scheme Procedure} assv-ref alist key\n"
196 "@deffnx {Scheme Procedure} assoc-ref alist key\n"
197 "Like @code{assq}, @code{assv} and @code{assoc}, except that only the\n"
198 "value associated with @var{key} in @var{alist} is returned. These\n"
199 "functions are equivalent to\n\n"
201 "(let ((ent (@var{associator} @var{key} @var{alist})))\n"
202 " (and ent (cdr ent)))\n"
204 "where @var{associator} is one of @code{assq}, @code{assv} or @code{assoc}.")
205 #define FUNC_NAME s_scm_assq_ref
209 handle
= scm_sloppy_assq (key
, alist
);
210 if (scm_is_pair (handle
))
212 return SCM_CDR (handle
);
219 SCM_DEFINE (scm_assv_ref
, "assv-ref", 2, 0, 0,
220 (SCM alist
, SCM key
),
221 "Behaves like @code{assq-ref} but uses @code{eqv?} for key comparison.")
222 #define FUNC_NAME s_scm_assv_ref
226 handle
= scm_sloppy_assv (key
, alist
);
227 if (scm_is_pair (handle
))
229 return SCM_CDR (handle
);
236 SCM_DEFINE (scm_assoc_ref
, "assoc-ref", 2, 0, 0,
237 (SCM alist
, SCM key
),
238 "Behaves like @code{assq-ref} but uses @code{equal?} for key comparison.")
239 #define FUNC_NAME s_scm_assoc_ref
243 handle
= scm_sloppy_assoc (key
, alist
);
244 if (scm_is_pair (handle
))
246 return SCM_CDR (handle
);
257 SCM_DEFINE (scm_assq_set_x
, "assq-set!", 3, 0, 0,
258 (SCM alist
, SCM key
, SCM val
),
259 "@deffnx {Scheme Procedure} assv-set! alist key value\n"
260 "@deffnx {Scheme Procedure} assoc-set! alist key value\n"
261 "Reassociate @var{key} in @var{alist} with @var{value}: find any existing\n"
262 "@var{alist} entry for @var{key} and associate it with the new\n"
263 "@var{value}. If @var{alist} does not contain an entry for @var{key},\n"
264 "add a new one. Return the (possibly new) alist.\n\n"
265 "These functions do not attempt to verify the structure of @var{alist},\n"
266 "and so may cause unusual results if passed an object that is not an\n"
268 #define FUNC_NAME s_scm_assq_set_x
272 handle
= scm_sloppy_assq (key
, alist
);
273 if (scm_is_pair (handle
))
275 SCM_SETCDR (handle
, val
);
279 return scm_acons (key
, val
, alist
);
283 SCM_DEFINE (scm_assv_set_x
, "assv-set!", 3, 0, 0,
284 (SCM alist
, SCM key
, SCM val
),
285 "Behaves like @code{assq-set!} but uses @code{eqv?} for key comparison.")
286 #define FUNC_NAME s_scm_assv_set_x
290 handle
= scm_sloppy_assv (key
, alist
);
291 if (scm_is_pair (handle
))
293 SCM_SETCDR (handle
, val
);
297 return scm_acons (key
, val
, alist
);
301 SCM_DEFINE (scm_assoc_set_x
, "assoc-set!", 3, 0, 0,
302 (SCM alist
, SCM key
, SCM val
),
303 "Behaves like @code{assq-set!} but uses @code{equal?} for key comparison.")
304 #define FUNC_NAME s_scm_assoc_set_x
308 handle
= scm_sloppy_assoc (key
, alist
);
309 if (scm_is_pair (handle
))
311 SCM_SETCDR (handle
, val
);
315 return scm_acons (key
, val
, alist
);
322 SCM_DEFINE (scm_assq_remove_x
, "assq-remove!", 2, 0, 0,
323 (SCM alist
, SCM key
),
324 "@deffnx {Scheme Procedure} assv-remove! alist key\n"
325 "@deffnx {Scheme Procedure} assoc-remove! alist key\n"
326 "Delete the first entry in @var{alist} associated with @var{key}, and return\n"
327 "the resulting alist.")
328 #define FUNC_NAME s_scm_assq_remove_x
332 handle
= scm_sloppy_assq (key
, alist
);
333 if (scm_is_pair (handle
))
334 alist
= scm_delq1_x (handle
, alist
);
341 SCM_DEFINE (scm_assv_remove_x
, "assv-remove!", 2, 0, 0,
342 (SCM alist
, SCM key
),
343 "Behaves like @code{assq-remove!} but uses @code{eqv?} for key comparison.")
344 #define FUNC_NAME s_scm_assv_remove_x
348 handle
= scm_sloppy_assv (key
, alist
);
349 if (scm_is_pair (handle
))
350 alist
= scm_delq1_x (handle
, alist
);
357 SCM_DEFINE (scm_assoc_remove_x
, "assoc-remove!", 2, 0, 0,
358 (SCM alist
, SCM key
),
359 "Behaves like @code{assq-remove!} but uses @code{equal?} for key comparison.")
360 #define FUNC_NAME s_scm_assoc_remove_x
364 handle
= scm_sloppy_assoc (key
, alist
);
365 if (scm_is_pair (handle
))
366 alist
= scm_delq1_x (handle
, alist
);
380 #include "libguile/alist.x"