1 /* Copyright (C) 1995, 96, 97, 98, 99, 2000, 2001 Free Software Foundation, Inc.
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2, or (at your option)
8 * This program 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
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this software; see the file COPYING. If not, write to
15 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
21 * The exception is that, if you link the GUILE library with other files
22 * to produce an executable, this does not by itself cause the
23 * resulting executable to be covered by the GNU General Public License.
24 * Your use of that executable is in no way restricted on account of
25 * linking the GUILE library code into it.
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
30 * This exception applies only to the code released by the
31 * Free Software Foundation under the name GUILE. If you copy
32 * code from other Free Software Foundation releases into a copy of
33 * GUILE, as the General Public License permits, the exception does
34 * not apply to the code that you add in this way. To avoid misleading
35 * anyone as to the status of such modified files, you must delete
36 * this exception notice from them.
38 * If you write modifications of your own for GUILE, it is your choice
39 * whether to permit this exception to apply to your modifications.
40 * If you do not wish that, delete this exception notice. */
44 #include "libguile/_scm.h"
45 #include "libguile/eq.h"
46 #include "libguile/list.h"
47 #include "libguile/lang.h"
49 #include "libguile/validate.h"
50 #include "libguile/alist.h"
54 SCM_DEFINE (scm_acons
, "acons", 3, 0, 0,
55 (SCM key
, SCM value
, SCM alist
),
56 "Add a new key-value pair to @var{alist}. A new pair is\n"
57 "created whose car is @var{key} and whose cdr is @var{value}, and the\n"
58 "pair is consed onto @var{alist}, and the new list is returned. This\n"
59 "function is @emph{not} destructive; @var{alist} is not modified.")
60 #define FUNC_NAME s_scm_acons
62 return scm_cell (SCM_UNPACK (scm_cell (SCM_UNPACK (key
),
70 SCM_DEFINE (scm_sloppy_assq
, "sloppy-assq", 2, 0, 0,
72 "Behaves like @code{assq} but does not do any error checking.\n"
73 "Recommended only for use in Guile internals.")
74 #define FUNC_NAME s_scm_sloppy_assq
76 for (; SCM_CONSP (alist
); alist
= SCM_CDR (alist
))
78 SCM tmp
= SCM_CAR (alist
);
79 if (SCM_CONSP (tmp
) && SCM_EQ_P (SCM_CAR (tmp
), key
))
88 SCM_DEFINE (scm_sloppy_assv
, "sloppy-assv", 2, 0, 0,
90 "Behaves like @code{assv} but does not do any error checking.\n"
91 "Recommended only for use in Guile internals.")
92 #define FUNC_NAME s_scm_sloppy_assv
94 for (; SCM_CONSP (alist
); alist
= SCM_CDR (alist
))
96 SCM tmp
= SCM_CAR (alist
);
98 && SCM_NFALSEP (scm_eqv_p (SCM_CAR (tmp
), key
)))
106 SCM_DEFINE (scm_sloppy_assoc
, "sloppy-assoc", 2, 0, 0,
107 (SCM key
, SCM alist
),
108 "Behaves like @code{assoc} but does not do any error checking.\n"
109 "Recommended only for use in Guile internals.")
110 #define FUNC_NAME s_scm_sloppy_assoc
112 for (; SCM_CONSP (alist
); alist
= SCM_CDR (alist
))
114 SCM tmp
= SCM_CAR (alist
);
116 && SCM_NFALSEP (scm_equal_p (SCM_CAR (tmp
), key
)))
126 SCM_DEFINE (scm_assq
, "assq", 2, 0, 0,
127 (SCM key
, SCM alist
),
128 "@deffnx {Scheme Procedure} assv key alist\n"
129 "@deffnx {Scheme Procedure} assoc key alist\n"
130 "Fetch the entry in @var{alist} that is associated with @var{key}. To\n"
131 "decide whether the argument @var{key} matches a particular entry in\n"
132 "@var{alist}, @code{assq} compares keys with @code{eq?}, @code{assv}\n"
133 "uses @code{eqv?} and @code{assoc} uses @code{equal?}. If @var{key}\n"
134 "cannot be found in @var{alist} (according to whichever equality\n"
135 "predicate is in use), then return @code{#f}. These functions\n"
136 "return the entire alist entry found (i.e. both the key and the value).")
137 #define FUNC_NAME s_scm_assq
140 for (; SCM_CONSP (ls
); ls
= SCM_CDR (ls
))
142 SCM tmp
= SCM_CAR (ls
);
143 SCM_ASSERT_TYPE (SCM_CONSP (tmp
), alist
, SCM_ARG2
, FUNC_NAME
,
145 if (SCM_EQ_P (SCM_CAR (tmp
), key
))
148 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (ls
), alist
, SCM_ARG2
, FUNC_NAME
,
155 SCM_DEFINE (scm_assv
, "assv", 2, 0, 0,
156 (SCM key
, SCM alist
),
157 "Behaves like @code{assq} but uses @code{eqv?} for key comparison.")
158 #define FUNC_NAME s_scm_assv
161 for(; SCM_CONSP (ls
); ls
= SCM_CDR (ls
))
163 SCM tmp
= SCM_CAR (ls
);
164 SCM_ASSERT_TYPE (SCM_CONSP (tmp
), alist
, SCM_ARG2
, FUNC_NAME
,
166 if (SCM_NFALSEP (scm_eqv_p (SCM_CAR (tmp
), key
)))
169 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (ls
), alist
, SCM_ARG2
, FUNC_NAME
,
176 SCM_DEFINE (scm_assoc
, "assoc", 2, 0, 0,
177 (SCM key
, SCM alist
),
178 "Behaves like @code{assq} but uses @code{equal?} for key comparison.")
179 #define FUNC_NAME s_scm_assoc
182 for(; SCM_CONSP (ls
); ls
= SCM_CDR (ls
))
184 SCM tmp
= SCM_CAR (ls
);
185 SCM_ASSERT_TYPE (SCM_CONSP (tmp
), alist
, SCM_ARG2
, FUNC_NAME
,
187 if (SCM_NFALSEP (scm_equal_p (SCM_CAR (tmp
), key
)))
190 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (ls
), alist
, SCM_ARG2
, FUNC_NAME
,
199 /* Dirk:API2.0:: We should not return #f if the key was not found. In the
200 * current solution we can not distinguish between finding a (key . #f) pair
201 * and not finding the key at all.
203 * Possible alternative solutions:
204 * 1) Remove assq-ref from the API: assq is sufficient.
205 * 2) Signal an error (what error type?) if the key is not found.
206 * 3) provide an additional 'default' parameter.
207 * 3.1) The default parameter is mandatory.
208 * 3.2) The default parameter is optional, but if no default is given and
209 * the key is not found, signal an error (what error type?).
211 SCM_DEFINE (scm_assq_ref
, "assq-ref", 2, 0, 0,
212 (SCM alist
, SCM key
),
213 "@deffnx {Scheme Procedure} assv-ref alist key\n"
214 "@deffnx {Scheme Procedure} assoc-ref alist key\n"
215 "Like @code{assq}, @code{assv} and @code{assoc}, except that only the\n"
216 "value associated with @var{key} in @var{alist} is returned. These\n"
217 "functions are equivalent to\n\n"
219 "(let ((ent (@var{associator} @var{key} @var{alist})))\n"
220 " (and ent (cdr ent)))\n"
222 "where @var{associator} is one of @code{assq}, @code{assv} or @code{assoc}.")
223 #define FUNC_NAME s_scm_assq_ref
227 handle
= scm_sloppy_assq (key
, alist
);
228 if (SCM_CONSP (handle
))
230 return SCM_CDR (handle
);
237 SCM_DEFINE (scm_assv_ref
, "assv-ref", 2, 0, 0,
238 (SCM alist
, SCM key
),
239 "Behaves like @code{assq-ref} but uses @code{eqv?} for key comparison.")
240 #define FUNC_NAME s_scm_assv_ref
244 handle
= scm_sloppy_assv (key
, alist
);
245 if (SCM_CONSP (handle
))
247 return SCM_CDR (handle
);
254 SCM_DEFINE (scm_assoc_ref
, "assoc-ref", 2, 0, 0,
255 (SCM alist
, SCM key
),
256 "Behaves like @code{assq-ref} but uses @code{equal?} for key comparison.")
257 #define FUNC_NAME s_scm_assoc_ref
261 handle
= scm_sloppy_assoc (key
, alist
);
262 if (SCM_CONSP (handle
))
264 return SCM_CDR (handle
);
275 SCM_DEFINE (scm_assq_set_x
, "assq-set!", 3, 0, 0,
276 (SCM alist
, SCM key
, SCM val
),
277 "@deffnx {Scheme Procedure} assv-set! alist key value\n"
278 "@deffnx {Scheme Procedure} assoc-set! alist key value\n"
279 "Reassociate @var{key} in @var{alist} with @var{value}: find any existing\n"
280 "@var{alist} entry for @var{key} and associate it with the new\n"
281 "@var{value}. If @var{alist} does not contain an entry for @var{key},\n"
282 "add a new one. Return the (possibly new) alist.\n\n"
283 "These functions do not attempt to verify the structure of @var{alist},\n"
284 "and so may cause unusual results if passed an object that is not an\n"
286 #define FUNC_NAME s_scm_assq_set_x
290 handle
= scm_sloppy_assq (key
, alist
);
291 if (SCM_CONSP (handle
))
293 SCM_SETCDR (handle
, val
);
297 return scm_acons (key
, val
, alist
);
301 SCM_DEFINE (scm_assv_set_x
, "assv-set!", 3, 0, 0,
302 (SCM alist
, SCM key
, SCM val
),
303 "Behaves like @code{assq-set!} but uses @code{eqv?} for key comparison.")
304 #define FUNC_NAME s_scm_assv_set_x
308 handle
= scm_sloppy_assv (key
, alist
);
309 if (SCM_CONSP (handle
))
311 SCM_SETCDR (handle
, val
);
315 return scm_acons (key
, val
, alist
);
319 SCM_DEFINE (scm_assoc_set_x
, "assoc-set!", 3, 0, 0,
320 (SCM alist
, SCM key
, SCM val
),
321 "Behaves like @code{assq-set!} but uses @code{equal?} for key comparison.")
322 #define FUNC_NAME s_scm_assoc_set_x
326 handle
= scm_sloppy_assoc (key
, alist
);
327 if (SCM_CONSP (handle
))
329 SCM_SETCDR (handle
, val
);
333 return scm_acons (key
, val
, alist
);
340 SCM_DEFINE (scm_assq_remove_x
, "assq-remove!", 2, 0, 0,
341 (SCM alist
, SCM key
),
342 "@deffnx {Scheme Procedure} assv-remove! alist key\n"
343 "@deffnx {Scheme Procedure} assoc-remove! alist key\n"
344 "Delete the first entry in @var{alist} associated with @var{key}, and return\n"
345 "the resulting alist.")
346 #define FUNC_NAME s_scm_assq_remove_x
350 handle
= scm_sloppy_assq (key
, alist
);
351 if (SCM_CONSP (handle
))
352 alist
= scm_delq1_x (handle
, alist
);
359 SCM_DEFINE (scm_assv_remove_x
, "assv-remove!", 2, 0, 0,
360 (SCM alist
, SCM key
),
361 "Behaves like @code{assq-remove!} but uses @code{eqv?} for key comparison.")
362 #define FUNC_NAME s_scm_assv_remove_x
366 handle
= scm_sloppy_assv (key
, alist
);
367 if (SCM_CONSP (handle
))
368 alist
= scm_delq1_x (handle
, alist
);
375 SCM_DEFINE (scm_assoc_remove_x
, "assoc-remove!", 2, 0, 0,
376 (SCM alist
, SCM key
),
377 "Behaves like @code{assq-remove!} but uses @code{equal?} for key comparison.")
378 #define FUNC_NAME s_scm_assoc_remove_x
382 handle
= scm_sloppy_assoc (key
, alist
);
383 if (SCM_CONSP (handle
))
384 alist
= scm_delq1_x (handle
, alist
);
398 #include "libguile/alist.x"