list.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946
  1. /* Copyright (C) 1995,1996,1997,2000,2001,2003,2004,2008
  2. * Free Software Foundation, Inc.
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #ifdef HAVE_CONFIG_H
  19. # include <config.h>
  20. #endif
  21. #include "libguile/_scm.h"
  22. #include "libguile/eq.h"
  23. #include "libguile/lang.h"
  24. #include "libguile/validate.h"
  25. #include "libguile/list.h"
  26. #include "libguile/eval.h"
  27. #include <stdarg.h>
  28. /* creating lists */
  29. #define SCM_I_CONS(cell, x, y) \
  30. do { \
  31. cell = scm_cell ((scm_t_bits)x, (scm_t_bits)y); \
  32. } while (0)
  33. SCM
  34. scm_list_1 (SCM e1)
  35. {
  36. SCM c1;
  37. SCM_I_CONS (c1, e1, SCM_EOL);
  38. return c1;
  39. }
  40. SCM
  41. scm_list_2 (SCM e1, SCM e2)
  42. {
  43. SCM c1, c2;
  44. SCM_I_CONS (c2, e2, SCM_EOL);
  45. SCM_I_CONS (c1, e1, c2);
  46. return c1;
  47. }
  48. SCM
  49. scm_list_3 (SCM e1, SCM e2, SCM e3)
  50. {
  51. SCM c1, c2, c3;
  52. SCM_I_CONS (c3, e3, SCM_EOL);
  53. SCM_I_CONS (c2, e2, c3);
  54. SCM_I_CONS (c1, e1, c2);
  55. return c1;
  56. }
  57. SCM
  58. scm_list_4 (SCM e1, SCM e2, SCM e3, SCM e4)
  59. {
  60. return scm_cons2 (e1, e2, scm_list_2 (e3, e4));
  61. }
  62. SCM
  63. scm_list_5 (SCM e1, SCM e2, SCM e3, SCM e4, SCM e5)
  64. {
  65. return scm_cons2 (e1, e2, scm_list_3 (e3, e4, e5));
  66. }
  67. SCM
  68. scm_list_n (SCM elt, ...)
  69. {
  70. va_list foo;
  71. SCM answer = SCM_EOL;
  72. SCM *pos = &answer;
  73. va_start (foo, elt);
  74. while (! SCM_UNBNDP (elt))
  75. {
  76. #if (SCM_DEBUG_CELL_ACCESSES == 1)
  77. if (SCM_NIMP (elt))
  78. SCM_VALIDATE_CELL(elt, 0);
  79. #endif
  80. *pos = scm_cons (elt, SCM_EOL);
  81. pos = SCM_CDRLOC (*pos);
  82. elt = va_arg (foo, SCM);
  83. }
  84. va_end (foo);
  85. return answer;
  86. }
  87. SCM_DEFINE (scm_make_list, "make-list", 1, 1, 0,
  88. (SCM n, SCM init),
  89. "Create a list containing of @var{n} elements, where each\n"
  90. "element is initialized to @var{init}. @var{init} defaults to\n"
  91. "the empty list @code{()} if not given.")
  92. #define FUNC_NAME s_scm_make_list
  93. {
  94. unsigned nn = scm_to_uint (n);
  95. unsigned i;
  96. SCM ret = SCM_EOL;
  97. if (SCM_UNBNDP (init))
  98. init = SCM_EOL;
  99. for (i = 0; i < nn; i++)
  100. ret = scm_cons (init, ret);
  101. return ret;
  102. }
  103. #undef FUNC_NAME
  104. SCM_DEFINE (scm_cons_star, "cons*", 1, 0, 1,
  105. (SCM arg, SCM rest),
  106. "Like @code{list}, but the last arg provides the tail of the\n"
  107. "constructed list, returning @code{(cons @var{arg1} (cons\n"
  108. "@var{arg2} (cons @dots{} @var{argn})))}. Requires at least one\n"
  109. "argument. If given one argument, that argument is returned as\n"
  110. "result. This function is called @code{list*} in some other\n"
  111. "Schemes and in Common LISP.")
  112. #define FUNC_NAME s_scm_cons_star
  113. {
  114. SCM ret = SCM_EOL;
  115. SCM *p = &ret;
  116. SCM_VALIDATE_REST_ARGUMENT (rest);
  117. for ( ; scm_is_pair (rest); rest = SCM_CDR (rest))
  118. {
  119. *p = scm_cons (arg, SCM_EOL);
  120. p = SCM_CDRLOC (*p);
  121. arg = SCM_CAR (rest);
  122. }
  123. *p = arg;
  124. return ret;
  125. }
  126. #undef FUNC_NAME
  127. /* general questions about lists --- null?, list?, length, etc. */
  128. SCM_DEFINE (scm_null_p, "null?", 1, 0, 0,
  129. (SCM x),
  130. "Return @code{#t} iff @var{x} is the empty list, else @code{#f}.")
  131. #define FUNC_NAME s_scm_null_p
  132. {
  133. return scm_from_bool (SCM_NULL_OR_NIL_P (x));
  134. }
  135. #undef FUNC_NAME
  136. SCM_DEFINE (scm_list_p, "list?", 1, 0, 0,
  137. (SCM x),
  138. "Return @code{#t} iff @var{x} is a proper list, else @code{#f}.")
  139. #define FUNC_NAME s_scm_list_p
  140. {
  141. return scm_from_bool (scm_ilength (x) >= 0);
  142. }
  143. #undef FUNC_NAME
  144. /* Return the length of SX, or -1 if it's not a proper list.
  145. This uses the "tortoise and hare" algorithm to detect "infinitely
  146. long" lists (i.e. lists with cycles in their cdrs), and returns -1
  147. if it does find one. */
  148. long
  149. scm_ilength(SCM sx)
  150. {
  151. long i = 0;
  152. SCM tortoise = sx;
  153. SCM hare = sx;
  154. do {
  155. if (SCM_NULL_OR_NIL_P(hare)) return i;
  156. if (!scm_is_pair (hare)) return -1;
  157. hare = SCM_CDR(hare);
  158. i++;
  159. if (SCM_NULL_OR_NIL_P(hare)) return i;
  160. if (!scm_is_pair (hare)) return -1;
  161. hare = SCM_CDR(hare);
  162. i++;
  163. /* For every two steps the hare takes, the tortoise takes one. */
  164. tortoise = SCM_CDR(tortoise);
  165. }
  166. while (!scm_is_eq (hare, tortoise));
  167. /* If the tortoise ever catches the hare, then the list must contain
  168. a cycle. */
  169. return -1;
  170. }
  171. SCM_DEFINE (scm_length, "length", 1, 0, 0,
  172. (SCM lst),
  173. "Return the number of elements in list @var{lst}.")
  174. #define FUNC_NAME s_scm_length
  175. {
  176. long i;
  177. SCM_VALIDATE_LIST_COPYLEN (1, lst, i);
  178. return scm_from_long (i);
  179. }
  180. #undef FUNC_NAME
  181. /* appending lists */
  182. SCM_DEFINE (scm_append, "append", 0, 0, 1,
  183. (SCM args),
  184. "Return a list consisting of the elements the lists passed as\n"
  185. "arguments.\n"
  186. "@lisp\n"
  187. "(append '(x) '(y)) @result{} (x y)\n"
  188. "(append '(a) '(b c d)) @result{} (a b c d)\n"
  189. "(append '(a (b)) '((c))) @result{} (a (b) (c))\n"
  190. "@end lisp\n"
  191. "The resulting list is always newly allocated, except that it\n"
  192. "shares structure with the last list argument. The last\n"
  193. "argument may actually be any object; an improper list results\n"
  194. "if the last argument is not a proper list.\n"
  195. "@lisp\n"
  196. "(append '(a b) '(c . d)) @result{} (a b c . d)\n"
  197. "(append '() 'a) @result{} a\n"
  198. "@end lisp")
  199. #define FUNC_NAME s_scm_append
  200. {
  201. SCM_VALIDATE_REST_ARGUMENT (args);
  202. if (scm_is_null (args)) {
  203. return SCM_EOL;
  204. } else {
  205. SCM res = SCM_EOL;
  206. SCM *lloc = &res;
  207. SCM arg = SCM_CAR (args);
  208. int argnum = 1;
  209. args = SCM_CDR (args);
  210. while (!scm_is_null (args)) {
  211. while (scm_is_pair (arg)) {
  212. *lloc = scm_cons (SCM_CAR (arg), SCM_EOL);
  213. lloc = SCM_CDRLOC (*lloc);
  214. arg = SCM_CDR (arg);
  215. }
  216. SCM_VALIDATE_NULL_OR_NIL (argnum, arg);
  217. arg = SCM_CAR (args);
  218. args = SCM_CDR (args);
  219. argnum++;
  220. };
  221. *lloc = arg;
  222. return res;
  223. }
  224. }
  225. #undef FUNC_NAME
  226. SCM_DEFINE (scm_append_x, "append!", 0, 0, 1,
  227. (SCM lists),
  228. "A destructive version of @code{append} (@pxref{Pairs and\n"
  229. "Lists,,,r5rs, The Revised^5 Report on Scheme}). The cdr field\n"
  230. "of each list's final pair is changed to point to the head of\n"
  231. "the next list, so no consing is performed. Return\n"
  232. "the mutated list.")
  233. #define FUNC_NAME s_scm_append_x
  234. {
  235. SCM ret, *loc;
  236. SCM_VALIDATE_REST_ARGUMENT (lists);
  237. if (scm_is_null (lists))
  238. return SCM_EOL;
  239. loc = &ret;
  240. for (;;)
  241. {
  242. SCM arg = SCM_CAR (lists);
  243. *loc = arg;
  244. lists = SCM_CDR (lists);
  245. if (scm_is_null (lists))
  246. return ret;
  247. if (!SCM_NULL_OR_NIL_P (arg))
  248. {
  249. SCM_VALIDATE_CONS (SCM_ARG1, arg);
  250. loc = SCM_CDRLOC (scm_last_pair (arg));
  251. }
  252. }
  253. }
  254. #undef FUNC_NAME
  255. SCM_DEFINE (scm_last_pair, "last-pair", 1, 0, 0,
  256. (SCM lst),
  257. "Return the last pair in @var{lst}, signalling an error if\n"
  258. "@var{lst} is circular.")
  259. #define FUNC_NAME s_scm_last_pair
  260. {
  261. SCM tortoise = lst;
  262. SCM hare = lst;
  263. if (SCM_NULL_OR_NIL_P (lst))
  264. return lst;
  265. SCM_VALIDATE_CONS (SCM_ARG1, lst);
  266. do {
  267. SCM ahead = SCM_CDR(hare);
  268. if (!scm_is_pair (ahead)) return hare;
  269. hare = ahead;
  270. ahead = SCM_CDR(hare);
  271. if (!scm_is_pair (ahead)) return hare;
  272. hare = ahead;
  273. tortoise = SCM_CDR(tortoise);
  274. }
  275. while (!scm_is_eq (hare, tortoise));
  276. SCM_MISC_ERROR ("Circular structure in position 1: ~S", scm_list_1 (lst));
  277. }
  278. #undef FUNC_NAME
  279. /* reversing lists */
  280. SCM_DEFINE (scm_reverse, "reverse", 1, 0, 0,
  281. (SCM lst),
  282. "Return a new list that contains the elements of @var{lst} but\n"
  283. "in reverse order.")
  284. #define FUNC_NAME s_scm_reverse
  285. {
  286. SCM result = SCM_EOL;
  287. SCM tortoise = lst;
  288. SCM hare = lst;
  289. do {
  290. if (SCM_NULL_OR_NIL_P(hare)) return result;
  291. SCM_ASSERT(scm_is_pair(hare), lst, 1, FUNC_NAME);
  292. result = scm_cons (SCM_CAR (hare), result);
  293. hare = SCM_CDR (hare);
  294. if (SCM_NULL_OR_NIL_P(hare)) return result;
  295. SCM_ASSERT(scm_is_pair(hare), lst, 1, FUNC_NAME);
  296. result = scm_cons (SCM_CAR (hare), result);
  297. hare = SCM_CDR (hare);
  298. tortoise = SCM_CDR (tortoise);
  299. }
  300. while (!scm_is_eq (hare, tortoise));
  301. SCM_MISC_ERROR ("Circular structure in position 1: ~S", scm_list_1 (lst));
  302. }
  303. #undef FUNC_NAME
  304. SCM_DEFINE (scm_reverse_x, "reverse!", 1, 1, 0,
  305. (SCM lst, SCM new_tail),
  306. "A destructive version of @code{reverse} (@pxref{Pairs and Lists,,,r5rs,\n"
  307. "The Revised^5 Report on Scheme}). The cdr of each cell in @var{lst} is\n"
  308. "modified to point to the previous list element. Return the\n"
  309. "reversed list.\n\n"
  310. "Caveat: because the list is modified in place, the tail of the original\n"
  311. "list now becomes its head, and the head of the original list now becomes\n"
  312. "the tail. Therefore, the @var{lst} symbol to which the head of the\n"
  313. "original list was bound now points to the tail. To ensure that the head\n"
  314. "of the modified list is not lost, it is wise to save the return value of\n"
  315. "@code{reverse!}")
  316. #define FUNC_NAME s_scm_reverse_x
  317. {
  318. SCM_VALIDATE_LIST (1, lst);
  319. if (SCM_UNBNDP (new_tail))
  320. new_tail = SCM_EOL;
  321. else
  322. SCM_VALIDATE_LIST (2, new_tail);
  323. while (!SCM_NULL_OR_NIL_P (lst))
  324. {
  325. SCM old_tail = SCM_CDR (lst);
  326. SCM_SETCDR (lst, new_tail);
  327. new_tail = lst;
  328. lst = old_tail;
  329. }
  330. return new_tail;
  331. }
  332. #undef FUNC_NAME
  333. /* indexing lists by element number */
  334. SCM_DEFINE (scm_list_ref, "list-ref", 2, 0, 0,
  335. (SCM list, SCM k),
  336. "Return the @var{k}th element from @var{list}.")
  337. #define FUNC_NAME s_scm_list_ref
  338. {
  339. SCM lst = list;
  340. unsigned long int i;
  341. i = scm_to_ulong (k);
  342. while (scm_is_pair (lst)) {
  343. if (i == 0)
  344. return SCM_CAR (lst);
  345. else {
  346. --i;
  347. lst = SCM_CDR (lst);
  348. }
  349. };
  350. if (SCM_NULL_OR_NIL_P (lst))
  351. SCM_OUT_OF_RANGE (2, k);
  352. else
  353. SCM_WRONG_TYPE_ARG (1, list);
  354. }
  355. #undef FUNC_NAME
  356. SCM_DEFINE (scm_list_set_x, "list-set!", 3, 0, 0,
  357. (SCM list, SCM k, SCM val),
  358. "Set the @var{k}th element of @var{list} to @var{val}.")
  359. #define FUNC_NAME s_scm_list_set_x
  360. {
  361. SCM lst = list;
  362. unsigned long int i = scm_to_ulong (k);
  363. while (scm_is_pair (lst)) {
  364. if (i == 0) {
  365. SCM_SETCAR (lst, val);
  366. return val;
  367. } else {
  368. --i;
  369. lst = SCM_CDR (lst);
  370. }
  371. };
  372. if (SCM_NULL_OR_NIL_P (lst))
  373. SCM_OUT_OF_RANGE (2, k);
  374. else
  375. SCM_WRONG_TYPE_ARG (1, list);
  376. }
  377. #undef FUNC_NAME
  378. SCM_REGISTER_PROC(s_list_cdr_ref, "list-cdr-ref", 2, 0, 0, scm_list_tail);
  379. SCM_DEFINE (scm_list_tail, "list-tail", 2, 0, 0,
  380. (SCM lst, SCM k),
  381. "@deffnx {Scheme Procedure} list-cdr-ref lst k\n"
  382. "Return the \"tail\" of @var{lst} beginning with its @var{k}th element.\n"
  383. "The first element of the list is considered to be element 0.\n\n"
  384. "@code{list-tail} and @code{list-cdr-ref} are identical. It may help to\n"
  385. "think of @code{list-cdr-ref} as accessing the @var{k}th cdr of the list,\n"
  386. "or returning the results of cdring @var{k} times down @var{lst}.")
  387. #define FUNC_NAME s_scm_list_tail
  388. {
  389. size_t i = scm_to_size_t (k);
  390. while (i-- > 0) {
  391. SCM_VALIDATE_CONS (1, lst);
  392. lst = SCM_CDR(lst);
  393. }
  394. return lst;
  395. }
  396. #undef FUNC_NAME
  397. SCM_DEFINE (scm_list_cdr_set_x, "list-cdr-set!", 3, 0, 0,
  398. (SCM list, SCM k, SCM val),
  399. "Set the @var{k}th cdr of @var{list} to @var{val}.")
  400. #define FUNC_NAME s_scm_list_cdr_set_x
  401. {
  402. SCM lst = list;
  403. size_t i = scm_to_size_t (k);
  404. while (scm_is_pair (lst)) {
  405. if (i == 0) {
  406. SCM_SETCDR (lst, val);
  407. return val;
  408. } else {
  409. --i;
  410. lst = SCM_CDR (lst);
  411. }
  412. };
  413. if (SCM_NULL_OR_NIL_P (lst))
  414. SCM_OUT_OF_RANGE (2, k);
  415. else
  416. SCM_WRONG_TYPE_ARG (1, list);
  417. }
  418. #undef FUNC_NAME
  419. /* copying lists, perhaps partially */
  420. SCM_DEFINE (scm_list_head, "list-head", 2, 0, 0,
  421. (SCM lst, SCM k),
  422. "Copy the first @var{k} elements from @var{lst} into a new list, and\n"
  423. "return it.")
  424. #define FUNC_NAME s_scm_list_head
  425. {
  426. SCM answer;
  427. SCM * pos;
  428. size_t i = scm_to_size_t (k);
  429. answer = SCM_EOL;
  430. pos = &answer;
  431. while (i-- > 0)
  432. {
  433. SCM_VALIDATE_CONS (1, lst);
  434. *pos = scm_cons (SCM_CAR (lst), SCM_EOL);
  435. pos = SCM_CDRLOC (*pos);
  436. lst = SCM_CDR(lst);
  437. }
  438. return answer;
  439. }
  440. #undef FUNC_NAME
  441. /* Copy a list which is known to be finite. The last pair may or may not have
  442. * a '() in its cdr. That is, improper lists are accepted. */
  443. SCM
  444. scm_i_finite_list_copy (SCM list)
  445. {
  446. if (!scm_is_pair (list))
  447. {
  448. return list;
  449. }
  450. else
  451. {
  452. SCM tail;
  453. const SCM result = tail = scm_list_1 (SCM_CAR (list));
  454. list = SCM_CDR (list);
  455. while (scm_is_pair (list))
  456. {
  457. const SCM new_tail = scm_list_1 (SCM_CAR (list));
  458. SCM_SETCDR (tail, new_tail);
  459. tail = new_tail;
  460. list = SCM_CDR (list);
  461. }
  462. SCM_SETCDR (tail, list);
  463. return result;
  464. }
  465. }
  466. SCM_DEFINE (scm_list_copy, "list-copy", 1, 0, 0,
  467. (SCM lst),
  468. "Return a (newly-created) copy of @var{lst}.")
  469. #define FUNC_NAME s_scm_list_copy
  470. {
  471. SCM newlst;
  472. SCM * fill_here;
  473. SCM from_here;
  474. SCM_VALIDATE_LIST (1, lst);
  475. newlst = SCM_EOL;
  476. fill_here = &newlst;
  477. from_here = lst;
  478. while (scm_is_pair (from_here))
  479. {
  480. SCM c;
  481. c = scm_cons (SCM_CAR (from_here), SCM_CDR (from_here));
  482. *fill_here = c;
  483. fill_here = SCM_CDRLOC (c);
  484. from_here = SCM_CDR (from_here);
  485. }
  486. return newlst;
  487. }
  488. #undef FUNC_NAME
  489. SCM_PROC (s_list, "list", 0, 0, 1, scm_list_copy);
  490. SCM_SNARF_DOCS (primitive, scm_list_copy, "list", (SCM objs), 0, 0, 1,
  491. "Return a list containing @var{objs}, the arguments to\n"
  492. "@code{list}.")
  493. /* This used to be the code for "list", but it's wrong when used via apply
  494. (it should copy the list). It seems pretty unlikely anyone would have
  495. been using this from C code, since it's a no-op, but keep it for strict
  496. binary compatibility. */
  497. SCM
  498. scm_list (SCM objs)
  499. {
  500. return objs;
  501. }
  502. /* membership tests (memq, memv, etc.) */
  503. /* The function scm_c_memq returns the first sublist of list whose car is
  504. * 'eq?' obj, where the sublists of list are the non-empty lists returned by
  505. * (list-tail list k) for k less than the length of list. If obj does not
  506. * occur in list, then #f (not the empty list) is returned.
  507. * List must be a proper list, otherwise scm_c_memq may crash or loop
  508. * endlessly.
  509. */
  510. SCM
  511. scm_c_memq (SCM obj, SCM list)
  512. {
  513. for (; !SCM_NULL_OR_NIL_P (list); list = SCM_CDR (list))
  514. {
  515. if (scm_is_eq (SCM_CAR (list), obj))
  516. return list;
  517. }
  518. return SCM_BOOL_F;
  519. }
  520. SCM_DEFINE (scm_memq, "memq", 2, 0, 0,
  521. (SCM x, SCM lst),
  522. "Return the first sublist of @var{lst} whose car is @code{eq?}\n"
  523. "to @var{x} where the sublists of @var{lst} are the non-empty\n"
  524. "lists returned by @code{(list-tail @var{lst} @var{k})} for\n"
  525. "@var{k} less than the length of @var{lst}. If @var{x} does not\n"
  526. "occur in @var{lst}, then @code{#f} (not the empty list) is\n"
  527. "returned.")
  528. #define FUNC_NAME s_scm_memq
  529. {
  530. SCM_VALIDATE_LIST (2, lst);
  531. return scm_c_memq (x, lst);
  532. }
  533. #undef FUNC_NAME
  534. SCM_DEFINE (scm_memv, "memv", 2, 0, 0,
  535. (SCM x, SCM lst),
  536. "Return the first sublist of @var{lst} whose car is @code{eqv?}\n"
  537. "to @var{x} where the sublists of @var{lst} are the non-empty\n"
  538. "lists returned by @code{(list-tail @var{lst} @var{k})} for\n"
  539. "@var{k} less than the length of @var{lst}. If @var{x} does not\n"
  540. "occur in @var{lst}, then @code{#f} (not the empty list) is\n"
  541. "returned.")
  542. #define FUNC_NAME s_scm_memv
  543. {
  544. SCM_VALIDATE_LIST (2, lst);
  545. for (; !SCM_NULL_OR_NIL_P (lst); lst = SCM_CDR (lst))
  546. {
  547. if (! scm_is_false (scm_eqv_p (SCM_CAR (lst), x)))
  548. return lst;
  549. }
  550. return SCM_BOOL_F;
  551. }
  552. #undef FUNC_NAME
  553. SCM_DEFINE (scm_member, "member", 2, 0, 0,
  554. (SCM x, SCM lst),
  555. "Return the first sublist of @var{lst} whose car is\n"
  556. "@code{equal?} to @var{x} where the sublists of @var{lst} are\n"
  557. "the non-empty lists returned by @code{(list-tail @var{lst}\n"
  558. "@var{k})} for @var{k} less than the length of @var{lst}. If\n"
  559. "@var{x} does not occur in @var{lst}, then @code{#f} (not the\n"
  560. "empty list) is returned.")
  561. #define FUNC_NAME s_scm_member
  562. {
  563. SCM_VALIDATE_LIST (2, lst);
  564. for (; !SCM_NULL_OR_NIL_P (lst); lst = SCM_CDR (lst))
  565. {
  566. if (! scm_is_false (scm_equal_p (SCM_CAR (lst), x)))
  567. return lst;
  568. }
  569. return SCM_BOOL_F;
  570. }
  571. #undef FUNC_NAME
  572. /* deleting elements from a list (delq, etc.) */
  573. SCM_DEFINE (scm_delq_x, "delq!", 2, 0, 0,
  574. (SCM item, SCM lst),
  575. "@deffnx {Scheme Procedure} delv! item lst\n"
  576. "@deffnx {Scheme Procedure} delete! item lst\n"
  577. "These procedures are destructive versions of @code{delq}, @code{delv}\n"
  578. "and @code{delete}: they modify the existing @var{lst}\n"
  579. "rather than creating a new list. Caveat evaluator: Like other\n"
  580. "destructive list functions, these functions cannot modify the binding of\n"
  581. "@var{lst}, and so cannot be used to delete the first element of\n"
  582. "@var{lst} destructively.")
  583. #define FUNC_NAME s_scm_delq_x
  584. {
  585. SCM walk;
  586. SCM *prev;
  587. for (prev = &lst, walk = lst;
  588. scm_is_pair (walk);
  589. walk = SCM_CDR (walk))
  590. {
  591. if (scm_is_eq (SCM_CAR (walk), item))
  592. *prev = SCM_CDR (walk);
  593. else
  594. prev = SCM_CDRLOC (walk);
  595. }
  596. return lst;
  597. }
  598. #undef FUNC_NAME
  599. SCM_DEFINE (scm_delv_x, "delv!", 2, 0, 0,
  600. (SCM item, SCM lst),
  601. "Destructively remove all elements from @var{lst} that are\n"
  602. "@code{eqv?} to @var{item}.")
  603. #define FUNC_NAME s_scm_delv_x
  604. {
  605. SCM walk;
  606. SCM *prev;
  607. for (prev = &lst, walk = lst;
  608. scm_is_pair (walk);
  609. walk = SCM_CDR (walk))
  610. {
  611. if (! scm_is_false (scm_eqv_p (SCM_CAR (walk), item)))
  612. *prev = SCM_CDR (walk);
  613. else
  614. prev = SCM_CDRLOC (walk);
  615. }
  616. return lst;
  617. }
  618. #undef FUNC_NAME
  619. SCM_DEFINE (scm_delete_x, "delete!", 2, 0, 0,
  620. (SCM item, SCM lst),
  621. "Destructively remove all elements from @var{lst} that are\n"
  622. "@code{equal?} to @var{item}.")
  623. #define FUNC_NAME s_scm_delete_x
  624. {
  625. SCM walk;
  626. SCM *prev;
  627. for (prev = &lst, walk = lst;
  628. scm_is_pair (walk);
  629. walk = SCM_CDR (walk))
  630. {
  631. if (! scm_is_false (scm_equal_p (SCM_CAR (walk), item)))
  632. *prev = SCM_CDR (walk);
  633. else
  634. prev = SCM_CDRLOC (walk);
  635. }
  636. return lst;
  637. }
  638. #undef FUNC_NAME
  639. SCM_DEFINE (scm_delq, "delq", 2, 0, 0,
  640. (SCM item, SCM lst),
  641. "Return a newly-created copy of @var{lst} with elements\n"
  642. "@code{eq?} to @var{item} removed. This procedure mirrors\n"
  643. "@code{memq}: @code{delq} compares elements of @var{lst} against\n"
  644. "@var{item} with @code{eq?}.")
  645. #define FUNC_NAME s_scm_delq
  646. {
  647. SCM copy = scm_list_copy (lst);
  648. return scm_delq_x (item, copy);
  649. }
  650. #undef FUNC_NAME
  651. SCM_DEFINE (scm_delv, "delv", 2, 0, 0,
  652. (SCM item, SCM lst),
  653. "Return a newly-created copy of @var{lst} with elements\n"
  654. "@code{eqv?} to @var{item} removed. This procedure mirrors\n"
  655. "@code{memv}: @code{delv} compares elements of @var{lst} against\n"
  656. "@var{item} with @code{eqv?}.")
  657. #define FUNC_NAME s_scm_delv
  658. {
  659. SCM copy = scm_list_copy (lst);
  660. return scm_delv_x (item, copy);
  661. }
  662. #undef FUNC_NAME
  663. SCM_DEFINE (scm_delete, "delete", 2, 0, 0,
  664. (SCM item, SCM lst),
  665. "Return a newly-created copy of @var{lst} with elements\n"
  666. "@code{equal?} to @var{item} removed. This procedure mirrors\n"
  667. "@code{member}: @code{delete} compares elements of @var{lst}\n"
  668. "against @var{item} with @code{equal?}.")
  669. #define FUNC_NAME s_scm_delete
  670. {
  671. SCM copy = scm_list_copy (lst);
  672. return scm_delete_x (item, copy);
  673. }
  674. #undef FUNC_NAME
  675. SCM_DEFINE (scm_delq1_x, "delq1!", 2, 0, 0,
  676. (SCM item, SCM lst),
  677. "Like @code{delq!}, but only deletes the first occurrence of\n"
  678. "@var{item} from @var{lst}. Tests for equality using\n"
  679. "@code{eq?}. See also @code{delv1!} and @code{delete1!}.")
  680. #define FUNC_NAME s_scm_delq1_x
  681. {
  682. SCM walk;
  683. SCM *prev;
  684. for (prev = &lst, walk = lst;
  685. scm_is_pair (walk);
  686. walk = SCM_CDR (walk))
  687. {
  688. if (scm_is_eq (SCM_CAR (walk), item))
  689. {
  690. *prev = SCM_CDR (walk);
  691. break;
  692. }
  693. else
  694. prev = SCM_CDRLOC (walk);
  695. }
  696. return lst;
  697. }
  698. #undef FUNC_NAME
  699. SCM_DEFINE (scm_delv1_x, "delv1!", 2, 0, 0,
  700. (SCM item, SCM lst),
  701. "Like @code{delv!}, but only deletes the first occurrence of\n"
  702. "@var{item} from @var{lst}. Tests for equality using\n"
  703. "@code{eqv?}. See also @code{delq1!} and @code{delete1!}.")
  704. #define FUNC_NAME s_scm_delv1_x
  705. {
  706. SCM walk;
  707. SCM *prev;
  708. for (prev = &lst, walk = lst;
  709. scm_is_pair (walk);
  710. walk = SCM_CDR (walk))
  711. {
  712. if (! scm_is_false (scm_eqv_p (SCM_CAR (walk), item)))
  713. {
  714. *prev = SCM_CDR (walk);
  715. break;
  716. }
  717. else
  718. prev = SCM_CDRLOC (walk);
  719. }
  720. return lst;
  721. }
  722. #undef FUNC_NAME
  723. SCM_DEFINE (scm_delete1_x, "delete1!", 2, 0, 0,
  724. (SCM item, SCM lst),
  725. "Like @code{delete!}, but only deletes the first occurrence of\n"
  726. "@var{item} from @var{lst}. Tests for equality using\n"
  727. "@code{equal?}. See also @code{delq1!} and @code{delv1!}.")
  728. #define FUNC_NAME s_scm_delete1_x
  729. {
  730. SCM walk;
  731. SCM *prev;
  732. for (prev = &lst, walk = lst;
  733. scm_is_pair (walk);
  734. walk = SCM_CDR (walk))
  735. {
  736. if (! scm_is_false (scm_equal_p (SCM_CAR (walk), item)))
  737. {
  738. *prev = SCM_CDR (walk);
  739. break;
  740. }
  741. else
  742. prev = SCM_CDRLOC (walk);
  743. }
  744. return lst;
  745. }
  746. #undef FUNC_NAME
  747. SCM_DEFINE (scm_filter, "filter", 2, 0, 0,
  748. (SCM pred, SCM list),
  749. "Return all the elements of 2nd arg @var{list} that satisfy predicate @var{pred}.\n"
  750. "The list is not disordered -- elements that appear in the result list occur\n"
  751. "in the same order as they occur in the argument list. The returned list may\n"
  752. "share a common tail with the argument list. The dynamic order in which the\n"
  753. "various applications of pred are made is not specified.\n\n"
  754. "@lisp\n"
  755. "(filter even? '(0 7 8 8 43 -4)) => (0 8 8 -4)\n"
  756. "@end lisp")
  757. #define FUNC_NAME s_scm_filter
  758. {
  759. scm_t_trampoline_1 call = scm_trampoline_1 (pred);
  760. SCM walk;
  761. SCM *prev;
  762. SCM res = SCM_EOL;
  763. SCM_ASSERT (call, pred, 1, FUNC_NAME);
  764. SCM_VALIDATE_LIST (2, list);
  765. for (prev = &res, walk = list;
  766. scm_is_pair (walk);
  767. walk = SCM_CDR (walk))
  768. {
  769. if (scm_is_true (call (pred, SCM_CAR (walk))))
  770. {
  771. *prev = scm_cons (SCM_CAR (walk), SCM_EOL);
  772. prev = SCM_CDRLOC (*prev);
  773. }
  774. }
  775. return res;
  776. }
  777. #undef FUNC_NAME
  778. SCM_DEFINE (scm_filter_x, "filter!", 2, 0, 0,
  779. (SCM pred, SCM list),
  780. "Linear-update variant of @code{filter}.")
  781. #define FUNC_NAME s_scm_filter_x
  782. {
  783. scm_t_trampoline_1 call = scm_trampoline_1 (pred);
  784. SCM walk;
  785. SCM *prev;
  786. SCM_ASSERT (call, pred, 1, FUNC_NAME);
  787. SCM_VALIDATE_LIST (2, list);
  788. for (prev = &list, walk = list;
  789. scm_is_pair (walk);
  790. walk = SCM_CDR (walk))
  791. {
  792. if (scm_is_true (call (pred, SCM_CAR (walk))))
  793. prev = SCM_CDRLOC (walk);
  794. else
  795. *prev = SCM_CDR (walk);
  796. }
  797. return list;
  798. }
  799. #undef FUNC_NAME
  800. void
  801. scm_init_list ()
  802. {
  803. #include "libguile/list.x"
  804. }
  805. /*
  806. Local Variables:
  807. c-file-style: "gnu"
  808. End:
  809. */