1 /* Copyright (C) 1995, 96, 97, 98, 99, 2000, 2001, 2004, 2006, 2008, 2010, 2011 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_cons (scm_cons (key
, value
), alist
);
49 SCM_DEFINE (scm_sloppy_assq
, "sloppy-assq", 2, 0, 0,
51 "Behaves like @code{assq} but does not do any error checking.\n"
52 "Recommended only for use in Guile internals.")
53 #define FUNC_NAME s_scm_sloppy_assq
55 for (; scm_is_pair (alist
); alist
= SCM_CDR (alist
))
57 SCM tmp
= SCM_CAR (alist
);
58 if (scm_is_pair (tmp
) && scm_is_eq (SCM_CAR (tmp
), key
))
67 SCM_DEFINE (scm_sloppy_assv
, "sloppy-assv", 2, 0, 0,
69 "Behaves like @code{assv} but does not do any error checking.\n"
70 "Recommended only for use in Guile internals.")
71 #define FUNC_NAME s_scm_sloppy_assv
73 for (; scm_is_pair (alist
); alist
= SCM_CDR (alist
))
75 SCM tmp
= SCM_CAR (alist
);
77 && scm_is_true (scm_eqv_p (SCM_CAR (tmp
), key
)))
85 SCM_DEFINE (scm_sloppy_assoc
, "sloppy-assoc", 2, 0, 0,
87 "Behaves like @code{assoc} but does not do any error checking.\n"
88 "Recommended only for use in Guile internals.")
89 #define FUNC_NAME s_scm_sloppy_assoc
91 for (; scm_is_pair (alist
); alist
= SCM_CDR (alist
))
93 SCM tmp
= SCM_CAR (alist
);
95 && scm_is_true (scm_equal_p (SCM_CAR (tmp
), key
)))
105 SCM_DEFINE (scm_assq
, "assq", 2, 0, 0,
106 (SCM key
, SCM alist
),
107 "@deffnx {Scheme Procedure} assv key alist\n"
108 "@deffnx {Scheme Procedure} assoc key alist\n"
109 "Fetch the entry in @var{alist} that is associated with @var{key}. To\n"
110 "decide whether the argument @var{key} matches a particular entry in\n"
111 "@var{alist}, @code{assq} compares keys with @code{eq?}, @code{assv}\n"
112 "uses @code{eqv?} and @code{assoc} uses @code{equal?}. If @var{key}\n"
113 "cannot be found in @var{alist} (according to whichever equality\n"
114 "predicate is in use), then return @code{#f}. These functions\n"
115 "return the entire alist entry found (i.e. both the key and the value).")
116 #define FUNC_NAME s_scm_assq
119 for(; scm_is_pair (ls
); ls
= SCM_CDR (ls
))
121 SCM tmp
= SCM_CAR (ls
);
122 SCM_ASSERT_TYPE (scm_is_pair (tmp
), alist
, SCM_ARG2
, FUNC_NAME
,
124 if (scm_is_eq (SCM_CAR (tmp
), key
))
127 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (ls
), alist
, SCM_ARG2
, FUNC_NAME
,
134 SCM_DEFINE (scm_assv
, "assv", 2, 0, 0,
135 (SCM key
, SCM alist
),
136 "Behaves like @code{assq} but uses @code{eqv?} for key comparison.")
137 #define FUNC_NAME s_scm_assv
140 for(; scm_is_pair (ls
); ls
= SCM_CDR (ls
))
142 SCM tmp
= SCM_CAR (ls
);
143 SCM_ASSERT_TYPE (scm_is_pair (tmp
), alist
, SCM_ARG2
, FUNC_NAME
,
145 if (scm_is_true (scm_eqv_p (SCM_CAR (tmp
), key
)))
148 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (ls
), alist
, SCM_ARG2
, FUNC_NAME
,
155 SCM_DEFINE (scm_assoc
, "assoc", 2, 0, 0,
156 (SCM key
, SCM alist
),
157 "Behaves like @code{assq} but uses @code{equal?} for key comparison.")
158 #define FUNC_NAME s_scm_assoc
161 for(; scm_is_pair (ls
); ls
= SCM_CDR (ls
))
163 SCM tmp
= SCM_CAR (ls
);
164 SCM_ASSERT_TYPE (scm_is_pair (tmp
), alist
, SCM_ARG2
, FUNC_NAME
,
166 if (scm_is_true (scm_equal_p (SCM_CAR (tmp
), key
)))
169 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (ls
), alist
, SCM_ARG2
, FUNC_NAME
,
178 /* Dirk:API2.0:: We should not return #f if the key was not found. In the
179 * current solution we can not distinguish between finding a (key . #f) pair
180 * and not finding the key at all.
182 * Possible alternative solutions:
183 * 1) Remove assq-ref from the API: assq is sufficient.
184 * 2) Signal an error (what error type?) if the key is not found.
185 * 3) provide an additional 'default' parameter.
186 * 3.1) The default parameter is mandatory.
187 * 3.2) The default parameter is optional, but if no default is given and
188 * the key is not found, signal an error (what error type?).
190 SCM_DEFINE (scm_assq_ref
, "assq-ref", 2, 0, 0,
191 (SCM alist
, SCM key
),
192 "@deffnx {Scheme Procedure} assv-ref alist key\n"
193 "@deffnx {Scheme Procedure} assoc-ref alist key\n"
194 "Like @code{assq}, @code{assv} and @code{assoc}, except that only the\n"
195 "value associated with @var{key} in @var{alist} is returned. These\n"
196 "functions are equivalent to\n\n"
198 "(let ((ent (@var{associator} @var{key} @var{alist})))\n"
199 " (and ent (cdr ent)))\n"
201 "where @var{associator} is one of @code{assq}, @code{assv} or @code{assoc}.")
202 #define FUNC_NAME s_scm_assq_ref
206 handle
= scm_sloppy_assq (key
, alist
);
207 if (scm_is_pair (handle
))
209 return SCM_CDR (handle
);
216 SCM_DEFINE (scm_assv_ref
, "assv-ref", 2, 0, 0,
217 (SCM alist
, SCM key
),
218 "Behaves like @code{assq-ref} but uses @code{eqv?} for key comparison.")
219 #define FUNC_NAME s_scm_assv_ref
223 handle
= scm_sloppy_assv (key
, alist
);
224 if (scm_is_pair (handle
))
226 return SCM_CDR (handle
);
233 SCM_DEFINE (scm_assoc_ref
, "assoc-ref", 2, 0, 0,
234 (SCM alist
, SCM key
),
235 "Behaves like @code{assq-ref} but uses @code{equal?} for key comparison.")
236 #define FUNC_NAME s_scm_assoc_ref
240 handle
= scm_sloppy_assoc (key
, alist
);
241 if (scm_is_pair (handle
))
243 return SCM_CDR (handle
);
254 SCM_DEFINE (scm_assq_set_x
, "assq-set!", 3, 0, 0,
255 (SCM alist
, SCM key
, SCM val
),
256 "@deffnx {Scheme Procedure} assv-set! alist key value\n"
257 "@deffnx {Scheme Procedure} assoc-set! alist key value\n"
258 "Reassociate @var{key} in @var{alist} with @var{val}: find any existing\n"
259 "@var{alist} entry for @var{key} and associate it with the new\n"
260 "@var{val}. If @var{alist} does not contain an entry for @var{key},\n"
261 "add a new one. Return the (possibly new) alist.\n\n"
262 "These functions do not attempt to verify the structure of @var{alist},\n"
263 "and so may cause unusual results if passed an object that is not an\n"
265 #define FUNC_NAME s_scm_assq_set_x
269 handle
= scm_sloppy_assq (key
, alist
);
270 if (scm_is_pair (handle
))
272 SCM_SETCDR (handle
, val
);
276 return scm_acons (key
, val
, alist
);
280 SCM_DEFINE (scm_assv_set_x
, "assv-set!", 3, 0, 0,
281 (SCM alist
, SCM key
, SCM val
),
282 "Behaves like @code{assq-set!} but uses @code{eqv?} for key comparison.")
283 #define FUNC_NAME s_scm_assv_set_x
287 handle
= scm_sloppy_assv (key
, alist
);
288 if (scm_is_pair (handle
))
290 SCM_SETCDR (handle
, val
);
294 return scm_acons (key
, val
, alist
);
298 SCM_DEFINE (scm_assoc_set_x
, "assoc-set!", 3, 0, 0,
299 (SCM alist
, SCM key
, SCM val
),
300 "Behaves like @code{assq-set!} but uses @code{equal?} for key comparison.")
301 #define FUNC_NAME s_scm_assoc_set_x
305 handle
= scm_sloppy_assoc (key
, alist
);
306 if (scm_is_pair (handle
))
308 SCM_SETCDR (handle
, val
);
312 return scm_acons (key
, val
, alist
);
319 SCM_DEFINE (scm_assq_remove_x
, "assq-remove!", 2, 0, 0,
320 (SCM alist
, SCM key
),
321 "@deffnx {Scheme Procedure} assv-remove! alist key\n"
322 "@deffnx {Scheme Procedure} assoc-remove! alist key\n"
323 "Delete the first entry in @var{alist} associated with @var{key}, and return\n"
324 "the resulting alist.")
325 #define FUNC_NAME s_scm_assq_remove_x
329 handle
= scm_sloppy_assq (key
, alist
);
330 if (scm_is_pair (handle
))
331 alist
= scm_delq1_x (handle
, alist
);
338 SCM_DEFINE (scm_assv_remove_x
, "assv-remove!", 2, 0, 0,
339 (SCM alist
, SCM key
),
340 "Behaves like @code{assq-remove!} but uses @code{eqv?} for key comparison.")
341 #define FUNC_NAME s_scm_assv_remove_x
345 handle
= scm_sloppy_assv (key
, alist
);
346 if (scm_is_pair (handle
))
347 alist
= scm_delq1_x (handle
, alist
);
354 SCM_DEFINE (scm_assoc_remove_x
, "assoc-remove!", 2, 0, 0,
355 (SCM alist
, SCM key
),
356 "Behaves like @code{assq-remove!} but uses @code{equal?} for key comparison.")
357 #define FUNC_NAME s_scm_assoc_remove_x
361 handle
= scm_sloppy_assoc (key
, alist
);
362 if (scm_is_pair (handle
))
363 alist
= scm_delq1_x (handle
, alist
);
377 #include "libguile/alist.x"