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"
48 #include "libguile/validate.h"
49 #include "libguile/alist.h"
53 SCM_DEFINE (scm_acons
, "acons", 3, 0, 0,
54 (SCM key
, SCM value
, SCM alist
),
55 "Add a new key-value pair to @var{alist}. A new pair is\n"
56 "created whose car is @var{key} and whose cdr is @var{value}, and the\n"
57 "pair is consed onto @var{alist}, and the new list is returned. This\n"
58 "function is @emph{not} destructive; @var{alist} is not modified.")
59 #define FUNC_NAME s_scm_acons
65 SCM_SET_CELL_OBJECT_0 (pair
, key
);
66 SCM_SET_CELL_OBJECT_1 (pair
, value
);
69 SCM_SET_CELL_OBJECT_0 (head
, pair
);
70 SCM_SET_CELL_OBJECT_1 (head
, alist
);
78 SCM_DEFINE (scm_sloppy_assq
, "sloppy-assq", 2, 0, 0,
80 "Behaves like @code{assq} but does not do any error checking.\n"
81 "Recommended only for use in Guile internals.")
82 #define FUNC_NAME s_scm_sloppy_assq
84 for (; SCM_CONSP (alist
); alist
= SCM_CDR (alist
))
86 SCM tmp
= SCM_CAR (alist
);
87 if (SCM_CONSP (tmp
) && SCM_EQ_P (SCM_CAR (tmp
), key
))
96 SCM_DEFINE (scm_sloppy_assv
, "sloppy-assv", 2, 0, 0,
98 "Behaves like @code{assv} but does not do any error checking.\n"
99 "Recommended only for use in Guile internals.")
100 #define FUNC_NAME s_scm_sloppy_assv
102 for (; SCM_CONSP (alist
); alist
= SCM_CDR (alist
))
104 SCM tmp
= SCM_CAR (alist
);
106 && SCM_NFALSEP (scm_eqv_p (SCM_CAR (tmp
), key
)))
114 SCM_DEFINE (scm_sloppy_assoc
, "sloppy-assoc", 2, 0, 0,
115 (SCM key
, SCM alist
),
116 "Behaves like @code{assoc} but does not do any error checking.\n"
117 "Recommended only for use in Guile internals.")
118 #define FUNC_NAME s_scm_sloppy_assoc
120 for (; SCM_CONSP (alist
); alist
= SCM_CDR (alist
))
122 SCM tmp
= SCM_CAR (alist
);
124 && SCM_NFALSEP (scm_equal_p (SCM_CAR (tmp
), key
)))
134 SCM_DEFINE (scm_assq
, "assq", 2, 0, 0,
135 (SCM key
, SCM alist
),
136 "@deffnx {Scheme Procedure} assv key alist\n"
137 "@deffnx {Scheme Procedure} assoc key alist\n"
138 "Fetch the entry in @var{alist} that is associated with @var{key}. To\n"
139 "decide whether the argument @var{key} matches a particular entry in\n"
140 "@var{alist}, @code{assq} compares keys with @code{eq?}, @code{assv}\n"
141 "uses @code{eqv?} and @code{assoc} uses @code{equal?}. If @var{key}\n"
142 "cannot be found in @var{alist} (according to whichever equality\n"
143 "predicate is in use), then return @code{#f}. These functions\n"
144 "return the entire alist entry found (i.e. both the key and the value).")
145 #define FUNC_NAME s_scm_assq
148 for (; SCM_CONSP (ls
); ls
= SCM_CDR (ls
))
150 SCM tmp
= SCM_CAR (ls
);
151 SCM_ASSERT_TYPE (SCM_CONSP (tmp
), alist
, SCM_ARG2
, FUNC_NAME
,
153 if (SCM_EQ_P (SCM_CAR (tmp
), key
))
156 SCM_ASSERT_TYPE (SCM_NULLP (ls
), alist
, SCM_ARG2
, FUNC_NAME
,
163 SCM_DEFINE (scm_assv
, "assv", 2, 0, 0,
164 (SCM key
, SCM alist
),
165 "Behaves like @code{assq} but uses @code{eqv?} for key comparison.")
166 #define FUNC_NAME s_scm_assv
169 for(; SCM_CONSP (ls
); ls
= SCM_CDR (ls
))
171 SCM tmp
= SCM_CAR (ls
);
172 SCM_ASSERT_TYPE (SCM_CONSP (tmp
), alist
, SCM_ARG2
, FUNC_NAME
,
174 if (SCM_NFALSEP (scm_eqv_p (SCM_CAR (tmp
), key
)))
177 SCM_ASSERT_TYPE (SCM_NULLP (ls
), alist
, SCM_ARG2
, FUNC_NAME
,
184 SCM_DEFINE (scm_assoc
, "assoc", 2, 0, 0,
185 (SCM key
, SCM alist
),
186 "Behaves like @code{assq} but uses @code{equal?} for key comparison.")
187 #define FUNC_NAME s_scm_assoc
190 for(; SCM_CONSP (ls
); ls
= SCM_CDR (ls
))
192 SCM tmp
= SCM_CAR (ls
);
193 SCM_ASSERT_TYPE (SCM_CONSP (tmp
), alist
, SCM_ARG2
, FUNC_NAME
,
195 if (SCM_NFALSEP (scm_equal_p (SCM_CAR (tmp
), key
)))
198 SCM_ASSERT_TYPE (SCM_NULLP (ls
), alist
, SCM_ARG2
, FUNC_NAME
,
207 /* Dirk:API2.0:: We should not return #f if the key was not found. In the
208 * current solution we can not distinguish between finding a (key . #f) pair
209 * and not finding the key at all.
211 * Possible alternative solutions:
212 * 1) Remove assq-ref from the API: assq is sufficient.
213 * 2) Signal an error (what error type?) if the key is not found.
214 * 3) provide an additional 'default' parameter.
215 * 3.1) The default parameter is mandatory.
216 * 3.2) The default parameter is optional, but if no default is given and
217 * the key is not found, signal an error (what error type?).
219 SCM_DEFINE (scm_assq_ref
, "assq-ref", 2, 0, 0,
220 (SCM alist
, SCM key
),
221 "@deffnx {Scheme Procedure} assv-ref alist key\n"
222 "@deffnx {Scheme Procedure} assoc-ref alist key\n"
223 "Like @code{assq}, @code{assv} and @code{assoc}, except that only the\n"
224 "value associated with @var{key} in @var{alist} is returned. These\n"
225 "functions are equivalent to\n\n"
227 "(let ((ent (@var{associator} @var{key} @var{alist})))\n"
228 " (and ent (cdr ent)))\n"
230 "where @var{associator} is one of @code{assq}, @code{assv} or @code{assoc}.")
231 #define FUNC_NAME s_scm_assq_ref
235 handle
= scm_sloppy_assq (key
, alist
);
236 if (SCM_CONSP (handle
))
238 return SCM_CDR (handle
);
245 SCM_DEFINE (scm_assv_ref
, "assv-ref", 2, 0, 0,
246 (SCM alist
, SCM key
),
247 "Behaves like @code{assq-ref} but uses @code{eqv?} for key comparison.")
248 #define FUNC_NAME s_scm_assv_ref
252 handle
= scm_sloppy_assv (key
, alist
);
253 if (SCM_CONSP (handle
))
255 return SCM_CDR (handle
);
262 SCM_DEFINE (scm_assoc_ref
, "assoc-ref", 2, 0, 0,
263 (SCM alist
, SCM key
),
264 "Behaves like @code{assq-ref} but uses @code{equal?} for key comparison.")
265 #define FUNC_NAME s_scm_assoc_ref
269 handle
= scm_sloppy_assoc (key
, alist
);
270 if (SCM_CONSP (handle
))
272 return SCM_CDR (handle
);
283 SCM_DEFINE (scm_assq_set_x
, "assq-set!", 3, 0, 0,
284 (SCM alist
, SCM key
, SCM val
),
285 "@deffnx {Scheme Procedure} assv-set! alist key value\n"
286 "@deffnx {Scheme Procedure} assoc-set! alist key value\n"
287 "Reassociate @var{key} in @var{alist} with @var{value}: find any existing\n"
288 "@var{alist} entry for @var{key} and associate it with the new\n"
289 "@var{value}. If @var{alist} does not contain an entry for @var{key},\n"
290 "add a new one. Return the (possibly new) alist.\n\n"
291 "These functions do not attempt to verify the structure of @var{alist},\n"
292 "and so may cause unusual results if passed an object that is not an\n"
294 #define FUNC_NAME s_scm_assq_set_x
298 handle
= scm_sloppy_assq (key
, alist
);
299 if (SCM_CONSP (handle
))
301 SCM_SETCDR (handle
, val
);
305 return scm_acons (key
, val
, alist
);
309 SCM_DEFINE (scm_assv_set_x
, "assv-set!", 3, 0, 0,
310 (SCM alist
, SCM key
, SCM val
),
311 "Behaves like @code{assq-set!} but uses @code{eqv?} for key comparison.")
312 #define FUNC_NAME s_scm_assv_set_x
316 handle
= scm_sloppy_assv (key
, alist
);
317 if (SCM_CONSP (handle
))
319 SCM_SETCDR (handle
, val
);
323 return scm_acons (key
, val
, alist
);
327 SCM_DEFINE (scm_assoc_set_x
, "assoc-set!", 3, 0, 0,
328 (SCM alist
, SCM key
, SCM val
),
329 "Behaves like @code{assq-set!} but uses @code{equal?} for key comparison.")
330 #define FUNC_NAME s_scm_assoc_set_x
334 handle
= scm_sloppy_assoc (key
, alist
);
335 if (SCM_CONSP (handle
))
337 SCM_SETCDR (handle
, val
);
341 return scm_acons (key
, val
, alist
);
348 SCM_DEFINE (scm_assq_remove_x
, "assq-remove!", 2, 0, 0,
349 (SCM alist
, SCM key
),
350 "@deffnx {Scheme Procedure} assv-remove! alist key\n"
351 "@deffnx {Scheme Procedure} assoc-remove! alist key\n"
352 "Delete the first entry in @var{alist} associated with @var{key}, and return\n"
353 "the resulting alist.")
354 #define FUNC_NAME s_scm_assq_remove_x
358 handle
= scm_sloppy_assq (key
, alist
);
359 if (SCM_CONSP (handle
))
360 alist
= scm_delq1_x (handle
, alist
);
367 SCM_DEFINE (scm_assv_remove_x
, "assv-remove!", 2, 0, 0,
368 (SCM alist
, SCM key
),
369 "Behaves like @code{assq-remove!} but uses @code{eqv?} for key comparison.")
370 #define FUNC_NAME s_scm_assv_remove_x
374 handle
= scm_sloppy_assv (key
, alist
);
375 if (SCM_CONSP (handle
))
376 alist
= scm_delq1_x (handle
, alist
);
383 SCM_DEFINE (scm_assoc_remove_x
, "assoc-remove!", 2, 0, 0,
384 (SCM alist
, SCM key
),
385 "Behaves like @code{assq-remove!} but uses @code{equal?} for key comparison.")
386 #define FUNC_NAME s_scm_assoc_remove_x
390 handle
= scm_sloppy_assoc (key
, alist
);
391 if (SCM_CONSP (handle
))
392 alist
= scm_delq1_x (handle
, alist
);
406 #ifndef SCM_MAGIC_SNARFER
407 #include "libguile/alist.x"