1 /* Copyright (C) 1995, 96, 97, 98, 99, 2000, 2001, 2004, 2006, 2008, 2010 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"
29 #include "libguile/validate.h"
30 #include "libguile/pairs.h"
31 #include "libguile/alist.h"
35 SCM_DEFINE (scm_acons
, "acons", 3, 0, 0,
36 (SCM key
, SCM value
, SCM alist
),
37 "Add a new key-value pair to @var{alist}. A new pair is\n"
38 "created whose car is @var{key} and whose cdr is @var{value}, and the\n"
39 "pair is consed onto @var{alist}, and the new list is returned. This\n"
40 "function is @emph{not} destructive; @var{alist} is not modified.")
41 #define FUNC_NAME s_scm_acons
43 return scm_cell (SCM_UNPACK (scm_cell (SCM_UNPACK (key
),
51 SCM_DEFINE (scm_sloppy_assq
, "sloppy-assq", 2, 0, 0,
53 "Behaves like @code{assq} but does not do any error checking.\n"
54 "Recommended only for use in Guile internals.")
55 #define FUNC_NAME s_scm_sloppy_assq
57 for (; scm_is_pair (alist
); alist
= SCM_CDR (alist
))
59 SCM tmp
= SCM_CAR (alist
);
60 if (scm_is_pair (tmp
) && scm_is_eq (SCM_CAR (tmp
), key
))
69 SCM_DEFINE (scm_sloppy_assv
, "sloppy-assv", 2, 0, 0,
71 "Behaves like @code{assv} but does not do any error checking.\n"
72 "Recommended only for use in Guile internals.")
73 #define FUNC_NAME s_scm_sloppy_assv
75 for (; scm_is_pair (alist
); alist
= SCM_CDR (alist
))
77 SCM tmp
= SCM_CAR (alist
);
79 && scm_is_true (scm_eqv_p (SCM_CAR (tmp
), key
)))
87 SCM_DEFINE (scm_sloppy_assoc
, "sloppy-assoc", 2, 0, 0,
89 "Behaves like @code{assoc} but does not do any error checking.\n"
90 "Recommended only for use in Guile internals.")
91 #define FUNC_NAME s_scm_sloppy_assoc
93 for (; scm_is_pair (alist
); alist
= SCM_CDR (alist
))
95 SCM tmp
= SCM_CAR (alist
);
97 && scm_is_true (scm_equal_p (SCM_CAR (tmp
), key
)))
107 SCM_DEFINE (scm_assq
, "assq", 2, 0, 0,
108 (SCM key
, SCM alist
),
109 "@deffnx {Scheme Procedure} assv key alist\n"
110 "@deffnx {Scheme Procedure} assoc key alist\n"
111 "Fetch the entry in @var{alist} that is associated with @var{key}. To\n"
112 "decide whether the argument @var{key} matches a particular entry in\n"
113 "@var{alist}, @code{assq} compares keys with @code{eq?}, @code{assv}\n"
114 "uses @code{eqv?} and @code{assoc} uses @code{equal?}. If @var{key}\n"
115 "cannot be found in @var{alist} (according to whichever equality\n"
116 "predicate is in use), then return @code{#f}. These functions\n"
117 "return the entire alist entry found (i.e. both the key and the value).")
118 #define FUNC_NAME s_scm_assq
121 for(; scm_is_pair (ls
); ls
= SCM_CDR (ls
))
123 SCM tmp
= SCM_CAR (ls
);
124 SCM_ASSERT_TYPE (scm_is_pair (tmp
), alist
, SCM_ARG2
, FUNC_NAME
,
126 if (scm_is_eq (SCM_CAR (tmp
), key
))
129 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (ls
), alist
, SCM_ARG2
, FUNC_NAME
,
136 SCM_DEFINE (scm_assv
, "assv", 2, 0, 0,
137 (SCM key
, SCM alist
),
138 "Behaves like @code{assq} but uses @code{eqv?} for key comparison.")
139 #define FUNC_NAME s_scm_assv
142 for(; scm_is_pair (ls
); ls
= SCM_CDR (ls
))
144 SCM tmp
= SCM_CAR (ls
);
145 SCM_ASSERT_TYPE (scm_is_pair (tmp
), alist
, SCM_ARG2
, FUNC_NAME
,
147 if (scm_is_true (scm_eqv_p (SCM_CAR (tmp
), key
)))
150 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (ls
), alist
, SCM_ARG2
, FUNC_NAME
,
157 SCM_DEFINE (scm_assoc
, "assoc", 2, 0, 0,
158 (SCM key
, SCM alist
),
159 "Behaves like @code{assq} but uses @code{equal?} for key comparison.")
160 #define FUNC_NAME s_scm_assoc
163 for(; scm_is_pair (ls
); ls
= SCM_CDR (ls
))
165 SCM tmp
= SCM_CAR (ls
);
166 SCM_ASSERT_TYPE (scm_is_pair (tmp
), alist
, SCM_ARG2
, FUNC_NAME
,
168 if (scm_is_true (scm_equal_p (SCM_CAR (tmp
), key
)))
171 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (ls
), alist
, SCM_ARG2
, FUNC_NAME
,
180 /* Dirk:API2.0:: We should not return #f if the key was not found. In the
181 * current solution we can not distinguish between finding a (key . #f) pair
182 * and not finding the key at all.
184 * Possible alternative solutions:
185 * 1) Remove assq-ref from the API: assq is sufficient.
186 * 2) Signal an error (what error type?) if the key is not found.
187 * 3) provide an additional 'default' parameter.
188 * 3.1) The default parameter is mandatory.
189 * 3.2) The default parameter is optional, but if no default is given and
190 * the key is not found, signal an error (what error type?).
192 SCM_DEFINE (scm_assq_ref
, "assq-ref", 2, 0, 0,
193 (SCM alist
, SCM key
),
194 "@deffnx {Scheme Procedure} assv-ref alist key\n"
195 "@deffnx {Scheme Procedure} assoc-ref alist key\n"
196 "Like @code{assq}, @code{assv} and @code{assoc}, except that only the\n"
197 "value associated with @var{key} in @var{alist} is returned. These\n"
198 "functions are equivalent to\n\n"
200 "(let ((ent (@var{associator} @var{key} @var{alist})))\n"
201 " (and ent (cdr ent)))\n"
203 "where @var{associator} is one of @code{assq}, @code{assv} or @code{assoc}.")
204 #define FUNC_NAME s_scm_assq_ref
208 handle
= scm_sloppy_assq (key
, alist
);
209 if (scm_is_pair (handle
))
211 return SCM_CDR (handle
);
218 SCM_DEFINE (scm_assv_ref
, "assv-ref", 2, 0, 0,
219 (SCM alist
, SCM key
),
220 "Behaves like @code{assq-ref} but uses @code{eqv?} for key comparison.")
221 #define FUNC_NAME s_scm_assv_ref
225 handle
= scm_sloppy_assv (key
, alist
);
226 if (scm_is_pair (handle
))
228 return SCM_CDR (handle
);
235 SCM_DEFINE (scm_assoc_ref
, "assoc-ref", 2, 0, 0,
236 (SCM alist
, SCM key
),
237 "Behaves like @code{assq-ref} but uses @code{equal?} for key comparison.")
238 #define FUNC_NAME s_scm_assoc_ref
242 handle
= scm_sloppy_assoc (key
, alist
);
243 if (scm_is_pair (handle
))
245 return SCM_CDR (handle
);
256 SCM_DEFINE (scm_assq_set_x
, "assq-set!", 3, 0, 0,
257 (SCM alist
, SCM key
, SCM val
),
258 "@deffnx {Scheme Procedure} assv-set! alist key value\n"
259 "@deffnx {Scheme Procedure} assoc-set! alist key value\n"
260 "Reassociate @var{key} in @var{alist} with @var{value}: find any existing\n"
261 "@var{alist} entry for @var{key} and associate it with the new\n"
262 "@var{value}. If @var{alist} does not contain an entry for @var{key},\n"
263 "add a new one. Return the (possibly new) alist.\n\n"
264 "These functions do not attempt to verify the structure of @var{alist},\n"
265 "and so may cause unusual results if passed an object that is not an\n"
267 #define FUNC_NAME s_scm_assq_set_x
271 handle
= scm_sloppy_assq (key
, alist
);
272 if (scm_is_pair (handle
))
274 SCM_SETCDR (handle
, val
);
278 return scm_acons (key
, val
, alist
);
282 SCM_DEFINE (scm_assv_set_x
, "assv-set!", 3, 0, 0,
283 (SCM alist
, SCM key
, SCM val
),
284 "Behaves like @code{assq-set!} but uses @code{eqv?} for key comparison.")
285 #define FUNC_NAME s_scm_assv_set_x
289 handle
= scm_sloppy_assv (key
, alist
);
290 if (scm_is_pair (handle
))
292 SCM_SETCDR (handle
, val
);
296 return scm_acons (key
, val
, alist
);
300 SCM_DEFINE (scm_assoc_set_x
, "assoc-set!", 3, 0, 0,
301 (SCM alist
, SCM key
, SCM val
),
302 "Behaves like @code{assq-set!} but uses @code{equal?} for key comparison.")
303 #define FUNC_NAME s_scm_assoc_set_x
307 handle
= scm_sloppy_assoc (key
, alist
);
308 if (scm_is_pair (handle
))
310 SCM_SETCDR (handle
, val
);
314 return scm_acons (key
, val
, alist
);
321 SCM_DEFINE (scm_assq_remove_x
, "assq-remove!", 2, 0, 0,
322 (SCM alist
, SCM key
),
323 "@deffnx {Scheme Procedure} assv-remove! alist key\n"
324 "@deffnx {Scheme Procedure} assoc-remove! alist key\n"
325 "Delete the first entry in @var{alist} associated with @var{key}, and return\n"
326 "the resulting alist.")
327 #define FUNC_NAME s_scm_assq_remove_x
331 handle
= scm_sloppy_assq (key
, alist
);
332 if (scm_is_pair (handle
))
333 alist
= scm_delq1_x (handle
, alist
);
340 SCM_DEFINE (scm_assv_remove_x
, "assv-remove!", 2, 0, 0,
341 (SCM alist
, SCM key
),
342 "Behaves like @code{assq-remove!} but uses @code{eqv?} for key comparison.")
343 #define FUNC_NAME s_scm_assv_remove_x
347 handle
= scm_sloppy_assv (key
, alist
);
348 if (scm_is_pair (handle
))
349 alist
= scm_delq1_x (handle
, alist
);
356 SCM_DEFINE (scm_assoc_remove_x
, "assoc-remove!", 2, 0, 0,
357 (SCM alist
, SCM key
),
358 "Behaves like @code{assq-remove!} but uses @code{equal?} for key comparison.")
359 #define FUNC_NAME s_scm_assoc_remove_x
363 handle
= scm_sloppy_assoc (key
, alist
);
364 if (scm_is_pair (handle
))
365 alist
= scm_delq1_x (handle
, alist
);
379 #include "libguile/alist.x"