list.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  1. /* Copyright (C) 1995,1996,1997, 2000, 2002 Free Software Foundation, Inc.
  2. *
  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)
  6. * any later version.
  7. *
  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.
  12. *
  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
  17. *
  18. * As a special exception, the Free Software Foundation gives permission
  19. * for additional uses of the text contained in its release of GUILE.
  20. *
  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.
  26. *
  27. * This exception does not however invalidate any other reasons why
  28. * the executable file might be covered by the GNU General Public License.
  29. *
  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.
  37. *
  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. */
  41. #include <stdio.h>
  42. #include "libguile/_scm.h"
  43. #include "libguile/eq.h"
  44. #include "libguile/validate.h"
  45. #include "libguile/list.h"
  46. #ifdef __STDC__
  47. #include <stdarg.h>
  48. #define var_start(x, y) va_start(x, y)
  49. #else
  50. #include <varargs.h>
  51. #define var_start(x, y) va_start(x)
  52. #endif
  53. /* creating lists */
  54. SCM
  55. scm_listify (SCM elt, ...)
  56. {
  57. va_list foo;
  58. SCM answer = SCM_EOL;
  59. SCM *pos = &answer;
  60. var_start (foo, elt);
  61. while (! SCM_UNBNDP (elt))
  62. {
  63. *pos = scm_cons (elt, SCM_EOL);
  64. pos = SCM_CDRLOC (*pos);
  65. elt = va_arg (foo, SCM);
  66. }
  67. return answer;
  68. }
  69. SCM_DEFINE (scm_list, "list", 0, 0, 1,
  70. (SCM objs),
  71. "Return a list containing OBJS, the arguments to `list'.")
  72. #define FUNC_NAME s_scm_list
  73. {
  74. return objs;
  75. }
  76. #undef FUNC_NAME
  77. #if (SCM_DEBUG_DEPRECATED == 0)
  78. SCM_REGISTER_PROC (s_list_star, "list*", 1, 0, 1, scm_cons_star);
  79. #endif /* SCM_DEBUG_DEPRECATED == 0 */
  80. SCM_DEFINE (scm_cons_star, "cons*", 1, 0, 1,
  81. (SCM arg, SCM rest),
  82. "Like `list', but the last arg provides the tail of the constructed list,\n"
  83. "returning (cons ARG1 (cons ARG2 (cons ... ARGn))).\n"
  84. "Requires at least one argument. If given one argument, that argument\n"
  85. "is returned as result.\n"
  86. "This function is called `list*' in some other Schemes and in Common LISP.")
  87. #define FUNC_NAME s_scm_cons_star
  88. {
  89. SCM_VALIDATE_REST_ARGUMENT (rest);
  90. if (!SCM_NULLP (rest))
  91. {
  92. SCM prev = arg = scm_cons (arg, rest);
  93. while (SCM_NNULLP (SCM_CDR (rest)))
  94. {
  95. prev = rest;
  96. rest = SCM_CDR (rest);
  97. }
  98. SCM_SETCDR (prev, SCM_CAR (rest));
  99. }
  100. return arg;
  101. }
  102. #undef FUNC_NAME
  103. /* general questions about lists --- null?, list?, length, etc. */
  104. SCM_DEFINE (scm_null_p, "null?", 1, 0, 0,
  105. (SCM x),
  106. "Return #t iff X is the empty list, else #f.")
  107. #define FUNC_NAME s_scm_null_p
  108. {
  109. return SCM_BOOL (SCM_NULLP (x));
  110. }
  111. #undef FUNC_NAME
  112. SCM_DEFINE (scm_list_p, "list?", 1, 0, 0,
  113. (SCM x),
  114. "Return #t iff X is a proper list, else #f.")
  115. #define FUNC_NAME s_scm_list_p
  116. {
  117. return SCM_BOOL (scm_ilength (x) >= 0);
  118. }
  119. #undef FUNC_NAME
  120. /* Return the length of SX, or -1 if it's not a proper list.
  121. This uses the "tortoise and hare" algorithm to detect "infinitely
  122. long" lists (i.e. lists with cycles in their cdrs), and returns -1
  123. if it does find one. */
  124. long
  125. scm_ilength(SCM sx)
  126. {
  127. long i = 0;
  128. SCM tortoise = sx;
  129. SCM hare = sx;
  130. do {
  131. if (SCM_NULLP(hare)) return i;
  132. if (SCM_NCONSP(hare)) return -1;
  133. hare = SCM_CDR(hare);
  134. i++;
  135. if (SCM_NULLP(hare)) return i;
  136. if (SCM_NCONSP(hare)) return -1;
  137. hare = SCM_CDR(hare);
  138. i++;
  139. /* For every two steps the hare takes, the tortoise takes one. */
  140. tortoise = SCM_CDR(tortoise);
  141. }
  142. while (! SCM_EQ_P (hare, tortoise));
  143. /* If the tortoise ever catches the hare, then the list must contain
  144. a cycle. */
  145. return -1;
  146. }
  147. SCM_DEFINE (scm_length, "length", 1, 0, 0,
  148. (SCM lst),
  149. "Return the number of elements in list LST.")
  150. #define FUNC_NAME s_scm_length
  151. {
  152. int i;
  153. SCM_VALIDATE_LIST_COPYLEN (1,lst,i);
  154. return SCM_MAKINUM (i);
  155. }
  156. #undef FUNC_NAME
  157. /* appending lists */
  158. SCM_DEFINE (scm_append, "append", 0, 0, 1,
  159. (SCM args),
  160. "Returns a list consisting of the elements of the first LIST\n"
  161. "followed by the elements of the other LISTs.\n"
  162. "\n"
  163. " (append '(x) '(y)) => (x y)\n"
  164. " (append '(a) '(b c d)) => (a b c d)\n"
  165. " (append '(a (b)) '((c))) => (a (b) (c))\n"
  166. "\n"
  167. "The resulting list is always newly allocated, except that it shares\n"
  168. "structure with the last LIST argument. The last argument may\n"
  169. "actually be any object; an improper list results if the last\n"
  170. "argument is not a proper list.\n"
  171. " (append '(a b) '(c . d)) => (a b c . d)\n"
  172. " (append '() 'a) => a\n")
  173. #define FUNC_NAME s_scm_append
  174. {
  175. SCM_VALIDATE_REST_ARGUMENT (args);
  176. if (SCM_NULLP (args)) {
  177. return SCM_EOL;
  178. } else {
  179. SCM res = SCM_EOL;
  180. SCM *lloc = &res;
  181. SCM arg = SCM_CAR (args);
  182. args = SCM_CDR (args);
  183. while (!SCM_NULLP (args)) {
  184. while (SCM_CONSP (arg)) {
  185. *lloc = scm_cons (SCM_CAR (arg), SCM_EOL);
  186. lloc = SCM_CDRLOC (*lloc);
  187. arg = SCM_CDR (arg);
  188. }
  189. SCM_VALIDATE_NULL (SCM_ARGn, arg);
  190. arg = SCM_CAR (args);
  191. args = SCM_CDR (args);
  192. };
  193. *lloc = arg;
  194. return res;
  195. }
  196. }
  197. #undef FUNC_NAME
  198. SCM_DEFINE (scm_append_x, "append!", 0, 0, 1,
  199. (SCM args),
  200. "A destructive version of @code{append} (@pxref{Pairs and Lists,,,r4rs,\n"
  201. "The Revised^4 Report on Scheme}). The cdr field of each list's final\n"
  202. "pair is changed to point to the head of the next list, so no consing is\n"
  203. "performed. Return a pointer to the mutated list.")
  204. #define FUNC_NAME s_scm_append_x
  205. {
  206. SCM_VALIDATE_REST_ARGUMENT (args);
  207. while (1) {
  208. if (SCM_NULLP (args)) {
  209. return SCM_EOL;
  210. } else {
  211. SCM arg = SCM_CAR (args);
  212. args = SCM_CDR (args);
  213. if (SCM_NULLP (args)) {
  214. return arg;
  215. } else if (!SCM_NULLP (arg)) {
  216. SCM_VALIDATE_CONS (SCM_ARG1, arg);
  217. SCM_SETCDR (scm_last_pair (arg), scm_append_x (args));
  218. return arg;
  219. }
  220. }
  221. }
  222. }
  223. #undef FUNC_NAME
  224. SCM_DEFINE (scm_last_pair, "last-pair", 1, 0, 0,
  225. (SCM lst),
  226. "Return a pointer to the last pair in @var{lst}, signalling an error if\n"
  227. "@var{lst} is circular.")
  228. #define FUNC_NAME s_scm_last_pair
  229. {
  230. SCM tortoise = lst;
  231. SCM hare = lst;
  232. if (SCM_NULLP (lst))
  233. return SCM_EOL;
  234. SCM_VALIDATE_CONS (SCM_ARG1, lst);
  235. do {
  236. SCM ahead = SCM_CDR(hare);
  237. if (SCM_NCONSP(ahead)) return hare;
  238. hare = ahead;
  239. ahead = SCM_CDR(hare);
  240. if (SCM_NCONSP(ahead)) return hare;
  241. hare = ahead;
  242. tortoise = SCM_CDR(tortoise);
  243. }
  244. while (! SCM_EQ_P (hare, tortoise));
  245. SCM_MISC_ERROR ("Circular structure in position 1: ~S", SCM_LIST1 (lst));
  246. }
  247. #undef FUNC_NAME
  248. /* reversing lists */
  249. SCM_DEFINE (scm_reverse, "reverse", 1, 0, 0,
  250. (SCM lst),
  251. "Return a new list that contains the elements of LST but in reverse order.")
  252. #define FUNC_NAME s_scm_reverse
  253. {
  254. SCM result = SCM_EOL;
  255. SCM tortoise = lst;
  256. SCM hare = lst;
  257. do {
  258. if (SCM_NULLP(hare)) return result;
  259. SCM_ASSERT(SCM_CONSP(hare), lst, 1, FUNC_NAME);
  260. result = scm_cons (SCM_CAR (hare), result);
  261. hare = SCM_CDR (hare);
  262. if (SCM_NULLP(hare)) return result;
  263. SCM_ASSERT(SCM_CONSP(hare), lst, 1, FUNC_NAME);
  264. result = scm_cons (SCM_CAR (hare), result);
  265. hare = SCM_CDR (hare);
  266. tortoise = SCM_CDR (tortoise);
  267. }
  268. while (! SCM_EQ_P (hare, tortoise));
  269. SCM_MISC_ERROR ("Circular structure in position 1: ~S", SCM_LIST1 (lst));
  270. }
  271. #undef FUNC_NAME
  272. SCM_DEFINE (scm_reverse_x, "reverse!", 1, 1, 0,
  273. (SCM lst, SCM new_tail),
  274. "A destructive version of @code{reverse} (@pxref{Pairs and Lists,,,r4rs,\n"
  275. "The Revised^4 Report on Scheme}). The cdr of each cell in @var{lst} is\n"
  276. "modified to point to the previous list element. Return a pointer to the\n"
  277. "head of the reversed list.\n\n"
  278. "Caveat: because the list is modified in place, the tail of the original\n"
  279. "list now becomes its head, and the head of the original list now becomes\n"
  280. "the tail. Therefore, the @var{lst} symbol to which the head of the\n"
  281. "original list was bound now points to the tail. To ensure that the head\n"
  282. "of the modified list is not lost, it is wise to save the return value of\n"
  283. "@code{reverse!}")
  284. #define FUNC_NAME s_scm_reverse_x
  285. {
  286. SCM_ASSERT (scm_ilength (lst) >= 0, lst, SCM_ARG1, FUNC_NAME);
  287. if (SCM_UNBNDP (new_tail))
  288. new_tail = SCM_EOL;
  289. else
  290. SCM_ASSERT (scm_ilength (new_tail) >= 0, new_tail, SCM_ARG2, FUNC_NAME);
  291. while (SCM_NNULLP (lst))
  292. {
  293. SCM old_tail = SCM_CDR (lst);
  294. SCM_SETCDR (lst, new_tail);
  295. new_tail = lst;
  296. lst = old_tail;
  297. }
  298. return new_tail;
  299. }
  300. #undef FUNC_NAME
  301. /* indexing lists by element number */
  302. SCM_DEFINE (scm_list_ref, "list-ref", 2, 0, 0,
  303. (SCM lst, SCM k),
  304. "Return the Kth element from list LST.")
  305. #define FUNC_NAME s_scm_list_ref
  306. {
  307. register long i;
  308. SCM_VALIDATE_INUM_MIN_COPY (2,k,0,i);
  309. while (i-- > 0) {
  310. SCM_ASRTGO(SCM_CONSP(lst), erout);
  311. lst = SCM_CDR(lst);
  312. }
  313. erout:
  314. SCM_ASSERT(SCM_CONSP(lst),
  315. SCM_NULLP(lst)?k:lst, SCM_NULLP(lst)?SCM_OUTOFRANGE:SCM_ARG1, FUNC_NAME);
  316. return SCM_CAR(lst);
  317. }
  318. #undef FUNC_NAME
  319. SCM_DEFINE (scm_list_set_x, "list-set!", 3, 0, 0,
  320. (SCM lst, SCM k, SCM val),
  321. "Set the @var{k}th element of @var{lst} to @var{val}.")
  322. #define FUNC_NAME s_scm_list_set_x
  323. {
  324. register long i;
  325. SCM_VALIDATE_INUM_MIN_COPY (2,k,0,i);
  326. while (i-- > 0) {
  327. SCM_ASRTGO(SCM_CONSP(lst), erout);
  328. lst = SCM_CDR(lst);
  329. }
  330. erout:
  331. SCM_ASSERT(SCM_CONSP(lst),
  332. SCM_NULLP(lst)?k:lst, SCM_NULLP(lst)?SCM_OUTOFRANGE:SCM_ARG1, FUNC_NAME);
  333. SCM_SETCAR (lst, val);
  334. return val;
  335. }
  336. #undef FUNC_NAME
  337. SCM_REGISTER_PROC(s_list_cdr_ref, "list-cdr-ref", 2, 0, 0, scm_list_tail);
  338. SCM_DEFINE (scm_list_tail, "list-tail", 2, 0, 0,
  339. (SCM lst, SCM k),
  340. "Return the \"tail\" of @var{lst} beginning with its @var{k}th element.\n"
  341. "The first element of the list is considered to be element 0.\n\n"
  342. "@code{list-cdr-ref} and @code{list-tail} are identical. It may help to\n"
  343. "think of @code{list-cdr-ref} as accessing the @var{k}th cdr of the list,\n"
  344. "or returning the results of cdring @var{k} times down @var{lst}.")
  345. #define FUNC_NAME s_scm_list_tail
  346. {
  347. register long i;
  348. SCM_VALIDATE_INUM_MIN_COPY (2,k,0,i);
  349. while (i-- > 0) {
  350. SCM_VALIDATE_CONS (1,lst);
  351. lst = SCM_CDR(lst);
  352. }
  353. return lst;
  354. }
  355. #undef FUNC_NAME
  356. SCM_DEFINE (scm_list_cdr_set_x, "list-cdr-set!", 3, 0, 0,
  357. (SCM lst, SCM k, SCM val),
  358. "Set the @var{k}th cdr of @var{lst} to @var{val}.")
  359. #define FUNC_NAME s_scm_list_cdr_set_x
  360. {
  361. register long i;
  362. SCM_VALIDATE_INUM_MIN_COPY (2,k,0,i);
  363. while (i-- > 0) {
  364. SCM_ASRTGO(SCM_CONSP(lst), erout);
  365. lst = SCM_CDR(lst);
  366. }
  367. erout:
  368. SCM_ASSERT(SCM_CONSP(lst),
  369. SCM_NULLP(lst)?k:lst, SCM_NULLP(lst)?SCM_OUTOFRANGE:SCM_ARG1, FUNC_NAME);
  370. SCM_SETCDR (lst, val);
  371. return val;
  372. }
  373. #undef FUNC_NAME
  374. /* copying lists, perhaps partially */
  375. SCM_DEFINE (scm_list_head, "list-head", 2, 0, 0,
  376. (SCM lst, SCM k),
  377. "Copy the first @var{k} elements from @var{lst} into a new list, and\n"
  378. "return it.")
  379. #define FUNC_NAME s_scm_list_head
  380. {
  381. SCM answer;
  382. SCM * pos;
  383. register long i;
  384. SCM_VALIDATE_INUM_MIN_COPY (2,k,0,i);
  385. answer = SCM_EOL;
  386. pos = &answer;
  387. while (i-- > 0)
  388. {
  389. SCM_VALIDATE_CONS (1,lst);
  390. *pos = scm_cons (SCM_CAR (lst), SCM_EOL);
  391. pos = SCM_CDRLOC (*pos);
  392. lst = SCM_CDR(lst);
  393. }
  394. return answer;
  395. }
  396. #undef FUNC_NAME
  397. SCM_DEFINE (scm_list_copy, "list-copy", 1, 0, 0,
  398. (SCM lst),
  399. "Return a (newly-created) copy of @var{lst}.")
  400. #define FUNC_NAME s_scm_list_copy
  401. {
  402. SCM newlst;
  403. SCM * fill_here;
  404. SCM from_here;
  405. newlst = SCM_EOL;
  406. fill_here = &newlst;
  407. from_here = lst;
  408. while (SCM_CONSP (from_here))
  409. {
  410. SCM c;
  411. c = scm_cons (SCM_CAR (from_here), SCM_CDR (from_here));
  412. *fill_here = c;
  413. fill_here = SCM_CDRLOC (c);
  414. from_here = SCM_CDR (from_here);
  415. }
  416. return newlst;
  417. }
  418. #undef FUNC_NAME
  419. /* membership tests (memq, memv, etc.) */
  420. SCM_DEFINE (scm_sloppy_memq, "sloppy-memq", 2, 0, 0,
  421. (SCM x, SCM lst),
  422. "This procedure behaves like @code{memq}, but does no type or error checking.\n"
  423. "Its use is recommended only in writing Guile internals,\n"
  424. "not for high-level Scheme programs.")
  425. #define FUNC_NAME s_scm_sloppy_memq
  426. {
  427. for(; SCM_CONSP (lst); lst = SCM_CDR(lst))
  428. {
  429. if (SCM_EQ_P (SCM_CAR (lst), x))
  430. return lst;
  431. }
  432. return lst;
  433. }
  434. #undef FUNC_NAME
  435. SCM_DEFINE (scm_sloppy_memv, "sloppy-memv", 2, 0, 0,
  436. (SCM x, SCM lst),
  437. "This procedure behaves like @code{memv}, but does no type or error checking.\n"
  438. "Its use is recommended only in writing Guile internals,\n"
  439. "not for high-level Scheme programs.")
  440. #define FUNC_NAME s_scm_sloppy_memv
  441. {
  442. for(; SCM_CONSP (lst); lst = SCM_CDR(lst))
  443. {
  444. if (! SCM_FALSEP (scm_eqv_p (SCM_CAR (lst), x)))
  445. return lst;
  446. }
  447. return lst;
  448. }
  449. #undef FUNC_NAME
  450. SCM_DEFINE (scm_sloppy_member, "sloppy-member", 2, 0, 0,
  451. (SCM x, SCM lst),
  452. "This procedure behaves like @code{member}, but does no type or error checking.\n"
  453. "Its use is recommended only in writing Guile internals,\n"
  454. "not for high-level Scheme programs.")
  455. #define FUNC_NAME s_scm_sloppy_member
  456. {
  457. for(; SCM_CONSP (lst); lst = SCM_CDR(lst))
  458. {
  459. if (! SCM_FALSEP (scm_equal_p (SCM_CAR (lst), x)))
  460. return lst;
  461. }
  462. return lst;
  463. }
  464. #undef FUNC_NAME
  465. SCM_DEFINE (scm_memq, "memq", 2, 0, 0,
  466. (SCM x, SCM lst),
  467. "Return the first sublist of LST whose car is `eq?' to X\n"
  468. "where the sublists of LST are the non-empty lists returned\n"
  469. "by `(list-tail LST K)' for K less than the length of LST. If\n"
  470. "X does not occur in LST, then `#f' (not the empty list) is\n"
  471. "returned.")
  472. #define FUNC_NAME s_scm_memq
  473. {
  474. SCM answer;
  475. SCM_VALIDATE_LIST (2,lst);
  476. answer = scm_sloppy_memq (x, lst);
  477. return (SCM_NULLP (answer)) ? SCM_BOOL_F : answer;
  478. }
  479. #undef FUNC_NAME
  480. SCM_DEFINE (scm_memv, "memv", 2, 0, 0,
  481. (SCM x, SCM lst),
  482. "Return the first sublist of LST whose car is `eqv?' to X\n"
  483. "where the sublists of LST are the non-empty lists returned\n"
  484. "by `(list-tail LST K)' for K less than the length of LST. If\n"
  485. "X does not occur in LST, then `#f' (not the empty list) is\n"
  486. "returned.")
  487. #define FUNC_NAME s_scm_memv
  488. {
  489. SCM answer;
  490. SCM_VALIDATE_LIST (2,lst);
  491. answer = scm_sloppy_memv (x, lst);
  492. return (SCM_NULLP (answer)) ? SCM_BOOL_F : answer;
  493. }
  494. #undef FUNC_NAME
  495. SCM_DEFINE (scm_member, "member", 2, 0, 0,
  496. (SCM x, SCM lst),
  497. "Return the first sublist of LST whose car is `equal?' to X\n"
  498. "where the sublists of LST are the non-empty lists returned\n"
  499. "by `(list-tail LST K)' for K less than the length of LST. If\n"
  500. "X does not occur in LST, then `#f' (not the empty list) is\n"
  501. "returned.")
  502. #define FUNC_NAME s_scm_member
  503. {
  504. SCM answer;
  505. SCM_VALIDATE_LIST (2,lst);
  506. answer = scm_sloppy_member (x, lst);
  507. return (SCM_NULLP (answer)) ? SCM_BOOL_F : answer;
  508. }
  509. #undef FUNC_NAME
  510. /* deleting elements from a list (delq, etc.) */
  511. SCM_DEFINE (scm_delq_x, "delq!", 2, 0, 0,
  512. (SCM item, SCM lst),
  513. "@deffnx primitive delv! item lst\n"
  514. "@deffnx primitive delete! item lst\n"
  515. "These procedures are destructive versions of @code{delq}, @code{delv}\n"
  516. "and @code{delete}: they modify the pointers in the existing @var{lst}\n"
  517. "rather than creating a new list. Caveat evaluator: Like other\n"
  518. "destructive list functions, these functions cannot modify the binding of\n"
  519. "@var{lst}, and so cannot be used to delete the first element of\n"
  520. "@var{lst} destructively.")
  521. #define FUNC_NAME s_scm_delq_x
  522. {
  523. SCM walk;
  524. SCM *prev;
  525. for (prev = &lst, walk = lst;
  526. SCM_CONSP (walk);
  527. walk = SCM_CDR (walk))
  528. {
  529. if (SCM_EQ_P (SCM_CAR (walk), item))
  530. *prev = SCM_CDR (walk);
  531. else
  532. prev = SCM_CDRLOC (walk);
  533. }
  534. return lst;
  535. }
  536. #undef FUNC_NAME
  537. SCM_DEFINE (scm_delv_x, "delv!", 2, 0, 0,
  538. (SCM item, SCM lst),
  539. "Destructively remove all elements from LST that are `eqv?' to ITEM.")
  540. #define FUNC_NAME s_scm_delv_x
  541. {
  542. SCM walk;
  543. SCM *prev;
  544. for (prev = &lst, walk = lst;
  545. SCM_CONSP (walk);
  546. walk = SCM_CDR (walk))
  547. {
  548. if (! SCM_FALSEP (scm_eqv_p (SCM_CAR (walk), item)))
  549. *prev = SCM_CDR (walk);
  550. else
  551. prev = SCM_CDRLOC (walk);
  552. }
  553. return lst;
  554. }
  555. #undef FUNC_NAME
  556. SCM_DEFINE (scm_delete_x, "delete!", 2, 0, 0,
  557. (SCM item, SCM lst),
  558. "Destructively remove all elements from LST that are `equal?' to ITEM.")
  559. #define FUNC_NAME s_scm_delete_x
  560. {
  561. SCM walk;
  562. SCM *prev;
  563. for (prev = &lst, walk = lst;
  564. SCM_CONSP (walk);
  565. walk = SCM_CDR (walk))
  566. {
  567. if (! SCM_FALSEP (scm_equal_p (SCM_CAR (walk), item)))
  568. *prev = SCM_CDR (walk);
  569. else
  570. prev = SCM_CDRLOC (walk);
  571. }
  572. return lst;
  573. }
  574. #undef FUNC_NAME
  575. SCM_DEFINE (scm_delq, "delq", 2, 0, 0,
  576. (SCM item, SCM lst),
  577. "Return a newly-created copy of @var{lst} with elements `eq?' to @var{item} removed.\n"
  578. "This procedure mirrors @code{memq}:\n"
  579. "@code{delq} compares elements of @var{lst} against @var{item} with\n"
  580. "@code{eq?}.")
  581. #define FUNC_NAME s_scm_delq
  582. {
  583. SCM copy = scm_list_copy (lst);
  584. return scm_delq_x (item, copy);
  585. }
  586. #undef FUNC_NAME
  587. SCM_DEFINE (scm_delv, "delv", 2, 0, 0,
  588. (SCM item, SCM lst),
  589. "Return a newly-created copy of @var{lst} with elements `eqv?' to @var{item} removed.\n"
  590. "This procedure mirrors @code{memv}:\n"
  591. "@code{delv} compares elements of @var{lst} against @var{item} with\n"
  592. "@code{eqv?}.")
  593. #define FUNC_NAME s_scm_delv
  594. {
  595. SCM copy = scm_list_copy (lst);
  596. return scm_delv_x (item, copy);
  597. }
  598. #undef FUNC_NAME
  599. SCM_DEFINE (scm_delete, "delete", 2, 0, 0,
  600. (SCM item, SCM lst),
  601. "Return a newly-created copy of @var{lst} with elements `equal?' to @var{item} removed.\n"
  602. "This procedure mirrors @code{member}:\n"
  603. "@code{delete} compares elements of @var{lst} against @var{item} with\n"
  604. "@code{equal?}.")
  605. #define FUNC_NAME s_scm_delete
  606. {
  607. SCM copy = scm_list_copy (lst);
  608. return scm_delete_x (item, copy);
  609. }
  610. #undef FUNC_NAME
  611. SCM_DEFINE (scm_delq1_x, "delq1!", 2, 0, 0,
  612. (SCM item, SCM lst),
  613. "Like `delq!', but only deletes the first occurrence of ITEM from LST.\n"
  614. "Tests for equality using `eq?'. See also `delv1!' and `delete1!'.")
  615. #define FUNC_NAME s_scm_delq1_x
  616. {
  617. SCM walk;
  618. SCM *prev;
  619. for (prev = &lst, walk = lst;
  620. SCM_CONSP (walk);
  621. walk = SCM_CDR (walk))
  622. {
  623. if (SCM_EQ_P (SCM_CAR (walk), item))
  624. {
  625. *prev = SCM_CDR (walk);
  626. break;
  627. }
  628. else
  629. prev = SCM_CDRLOC (walk);
  630. }
  631. return lst;
  632. }
  633. #undef FUNC_NAME
  634. SCM_DEFINE (scm_delv1_x, "delv1!", 2, 0, 0,
  635. (SCM item, SCM lst),
  636. "Like `delv!', but only deletes the first occurrence of ITEM from LST.\n"
  637. "Tests for equality using `eqv?'. See also `delq1!' and `delete1!'.")
  638. #define FUNC_NAME s_scm_delv1_x
  639. {
  640. SCM walk;
  641. SCM *prev;
  642. for (prev = &lst, walk = lst;
  643. SCM_CONSP (walk);
  644. walk = SCM_CDR (walk))
  645. {
  646. if (! SCM_FALSEP (scm_eqv_p (SCM_CAR (walk), item)))
  647. {
  648. *prev = SCM_CDR (walk);
  649. break;
  650. }
  651. else
  652. prev = SCM_CDRLOC (walk);
  653. }
  654. return lst;
  655. }
  656. #undef FUNC_NAME
  657. SCM_DEFINE (scm_delete1_x, "delete1!", 2, 0, 0,
  658. (SCM item, SCM lst),
  659. "Like `delete!', but only deletes the first occurrence of ITEM from LST.\n"
  660. "Tests for equality using `equal?'. See also `delq1!' and `delv1!'.")
  661. #define FUNC_NAME s_scm_delete1_x
  662. {
  663. SCM walk;
  664. SCM *prev;
  665. for (prev = &lst, walk = lst;
  666. SCM_CONSP (walk);
  667. walk = SCM_CDR (walk))
  668. {
  669. if (! SCM_FALSEP (scm_equal_p (SCM_CAR (walk), item)))
  670. {
  671. *prev = SCM_CDR (walk);
  672. break;
  673. }
  674. else
  675. prev = SCM_CDRLOC (walk);
  676. }
  677. return lst;
  678. }
  679. #undef FUNC_NAME
  680. void
  681. scm_init_list ()
  682. {
  683. #include "libguile/list.x"
  684. }
  685. /*
  686. Local Variables:
  687. c-file-style: "gnu"
  688. End:
  689. */