list.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944
  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. while (!SCM_NULL_OR_NIL_P (lst))
  322. {
  323. SCM old_tail = SCM_CDR (lst);
  324. SCM_SETCDR (lst, new_tail);
  325. new_tail = lst;
  326. lst = old_tail;
  327. }
  328. return new_tail;
  329. }
  330. #undef FUNC_NAME
  331. /* indexing lists by element number */
  332. SCM_DEFINE (scm_list_ref, "list-ref", 2, 0, 0,
  333. (SCM list, SCM k),
  334. "Return the @var{k}th element from @var{list}.")
  335. #define FUNC_NAME s_scm_list_ref
  336. {
  337. SCM lst = list;
  338. unsigned long int i;
  339. i = scm_to_ulong (k);
  340. while (scm_is_pair (lst)) {
  341. if (i == 0)
  342. return SCM_CAR (lst);
  343. else {
  344. --i;
  345. lst = SCM_CDR (lst);
  346. }
  347. };
  348. if (SCM_NULL_OR_NIL_P (lst))
  349. SCM_OUT_OF_RANGE (2, k);
  350. else
  351. SCM_WRONG_TYPE_ARG (1, list);
  352. }
  353. #undef FUNC_NAME
  354. SCM_DEFINE (scm_list_set_x, "list-set!", 3, 0, 0,
  355. (SCM list, SCM k, SCM val),
  356. "Set the @var{k}th element of @var{list} to @var{val}.")
  357. #define FUNC_NAME s_scm_list_set_x
  358. {
  359. SCM lst = list;
  360. unsigned long int i = scm_to_ulong (k);
  361. while (scm_is_pair (lst)) {
  362. if (i == 0) {
  363. SCM_SETCAR (lst, val);
  364. return val;
  365. } else {
  366. --i;
  367. lst = SCM_CDR (lst);
  368. }
  369. };
  370. if (SCM_NULL_OR_NIL_P (lst))
  371. SCM_OUT_OF_RANGE (2, k);
  372. else
  373. SCM_WRONG_TYPE_ARG (1, list);
  374. }
  375. #undef FUNC_NAME
  376. SCM_REGISTER_PROC(s_list_cdr_ref, "list-cdr-ref", 2, 0, 0, scm_list_tail);
  377. SCM_DEFINE (scm_list_tail, "list-tail", 2, 0, 0,
  378. (SCM lst, SCM k),
  379. "@deffnx {Scheme Procedure} list-cdr-ref lst k\n"
  380. "Return the \"tail\" of @var{lst} beginning with its @var{k}th element.\n"
  381. "The first element of the list is considered to be element 0.\n\n"
  382. "@code{list-tail} and @code{list-cdr-ref} are identical. It may help to\n"
  383. "think of @code{list-cdr-ref} as accessing the @var{k}th cdr of the list,\n"
  384. "or returning the results of cdring @var{k} times down @var{lst}.")
  385. #define FUNC_NAME s_scm_list_tail
  386. {
  387. size_t i = scm_to_size_t (k);
  388. while (i-- > 0) {
  389. SCM_VALIDATE_CONS (1, lst);
  390. lst = SCM_CDR(lst);
  391. }
  392. return lst;
  393. }
  394. #undef FUNC_NAME
  395. SCM_DEFINE (scm_list_cdr_set_x, "list-cdr-set!", 3, 0, 0,
  396. (SCM list, SCM k, SCM val),
  397. "Set the @var{k}th cdr of @var{list} to @var{val}.")
  398. #define FUNC_NAME s_scm_list_cdr_set_x
  399. {
  400. SCM lst = list;
  401. size_t i = scm_to_size_t (k);
  402. while (scm_is_pair (lst)) {
  403. if (i == 0) {
  404. SCM_SETCDR (lst, val);
  405. return val;
  406. } else {
  407. --i;
  408. lst = SCM_CDR (lst);
  409. }
  410. };
  411. if (SCM_NULL_OR_NIL_P (lst))
  412. SCM_OUT_OF_RANGE (2, k);
  413. else
  414. SCM_WRONG_TYPE_ARG (1, list);
  415. }
  416. #undef FUNC_NAME
  417. /* copying lists, perhaps partially */
  418. SCM_DEFINE (scm_list_head, "list-head", 2, 0, 0,
  419. (SCM lst, SCM k),
  420. "Copy the first @var{k} elements from @var{lst} into a new list, and\n"
  421. "return it.")
  422. #define FUNC_NAME s_scm_list_head
  423. {
  424. SCM answer;
  425. SCM * pos;
  426. size_t i = scm_to_size_t (k);
  427. answer = SCM_EOL;
  428. pos = &answer;
  429. while (i-- > 0)
  430. {
  431. SCM_VALIDATE_CONS (1, lst);
  432. *pos = scm_cons (SCM_CAR (lst), SCM_EOL);
  433. pos = SCM_CDRLOC (*pos);
  434. lst = SCM_CDR(lst);
  435. }
  436. return answer;
  437. }
  438. #undef FUNC_NAME
  439. /* Copy a list which is known to be finite. The last pair may or may not have
  440. * a '() in its cdr. That is, improper lists are accepted. */
  441. SCM
  442. scm_i_finite_list_copy (SCM list)
  443. {
  444. if (!scm_is_pair (list))
  445. {
  446. return list;
  447. }
  448. else
  449. {
  450. SCM tail;
  451. const SCM result = tail = scm_list_1 (SCM_CAR (list));
  452. list = SCM_CDR (list);
  453. while (scm_is_pair (list))
  454. {
  455. const SCM new_tail = scm_list_1 (SCM_CAR (list));
  456. SCM_SETCDR (tail, new_tail);
  457. tail = new_tail;
  458. list = SCM_CDR (list);
  459. }
  460. SCM_SETCDR (tail, list);
  461. return result;
  462. }
  463. }
  464. SCM_DEFINE (scm_list_copy, "list-copy", 1, 0, 0,
  465. (SCM lst),
  466. "Return a (newly-created) copy of @var{lst}.")
  467. #define FUNC_NAME s_scm_list_copy
  468. {
  469. SCM newlst;
  470. SCM * fill_here;
  471. SCM from_here;
  472. SCM_VALIDATE_LIST (1, lst);
  473. newlst = SCM_EOL;
  474. fill_here = &newlst;
  475. from_here = lst;
  476. while (scm_is_pair (from_here))
  477. {
  478. SCM c;
  479. c = scm_cons (SCM_CAR (from_here), SCM_CDR (from_here));
  480. *fill_here = c;
  481. fill_here = SCM_CDRLOC (c);
  482. from_here = SCM_CDR (from_here);
  483. }
  484. return newlst;
  485. }
  486. #undef FUNC_NAME
  487. SCM_PROC (s_list, "list", 0, 0, 1, scm_list_copy);
  488. SCM_SNARF_DOCS (primitive, scm_list_copy, "list", (SCM objs), 0, 0, 1,
  489. "Return a list containing @var{objs}, the arguments to\n"
  490. "@code{list}.")
  491. /* This used to be the code for "list", but it's wrong when used via apply
  492. (it should copy the list). It seems pretty unlikely anyone would have
  493. been using this from C code, since it's a no-op, but keep it for strict
  494. binary compatibility. */
  495. SCM
  496. scm_list (SCM objs)
  497. {
  498. return objs;
  499. }
  500. /* membership tests (memq, memv, etc.) */
  501. /* The function scm_c_memq returns the first sublist of list whose car is
  502. * 'eq?' obj, where the sublists of list are the non-empty lists returned by
  503. * (list-tail list k) for k less than the length of list. If obj does not
  504. * occur in list, then #f (not the empty list) is returned.
  505. * List must be a proper list, otherwise scm_c_memq may crash or loop
  506. * endlessly.
  507. */
  508. SCM
  509. scm_c_memq (SCM obj, SCM list)
  510. {
  511. for (; !SCM_NULL_OR_NIL_P (list); list = SCM_CDR (list))
  512. {
  513. if (scm_is_eq (SCM_CAR (list), obj))
  514. return list;
  515. }
  516. return SCM_BOOL_F;
  517. }
  518. SCM_DEFINE (scm_memq, "memq", 2, 0, 0,
  519. (SCM x, SCM lst),
  520. "Return the first sublist of @var{lst} whose car is @code{eq?}\n"
  521. "to @var{x} where the sublists of @var{lst} are the non-empty\n"
  522. "lists returned by @code{(list-tail @var{lst} @var{k})} for\n"
  523. "@var{k} less than the length of @var{lst}. If @var{x} does not\n"
  524. "occur in @var{lst}, then @code{#f} (not the empty list) is\n"
  525. "returned.")
  526. #define FUNC_NAME s_scm_memq
  527. {
  528. SCM_VALIDATE_LIST (2, lst);
  529. return scm_c_memq (x, lst);
  530. }
  531. #undef FUNC_NAME
  532. SCM_DEFINE (scm_memv, "memv", 2, 0, 0,
  533. (SCM x, SCM lst),
  534. "Return the first sublist of @var{lst} whose car is @code{eqv?}\n"
  535. "to @var{x} where the sublists of @var{lst} are the non-empty\n"
  536. "lists returned by @code{(list-tail @var{lst} @var{k})} for\n"
  537. "@var{k} less than the length of @var{lst}. If @var{x} does not\n"
  538. "occur in @var{lst}, then @code{#f} (not the empty list) is\n"
  539. "returned.")
  540. #define FUNC_NAME s_scm_memv
  541. {
  542. SCM_VALIDATE_LIST (2, lst);
  543. for (; !SCM_NULL_OR_NIL_P (lst); lst = SCM_CDR (lst))
  544. {
  545. if (! scm_is_false (scm_eqv_p (SCM_CAR (lst), x)))
  546. return lst;
  547. }
  548. return SCM_BOOL_F;
  549. }
  550. #undef FUNC_NAME
  551. SCM_DEFINE (scm_member, "member", 2, 0, 0,
  552. (SCM x, SCM lst),
  553. "Return the first sublist of @var{lst} whose car is\n"
  554. "@code{equal?} to @var{x} where the sublists of @var{lst} are\n"
  555. "the non-empty lists returned by @code{(list-tail @var{lst}\n"
  556. "@var{k})} for @var{k} less than the length of @var{lst}. If\n"
  557. "@var{x} does not occur in @var{lst}, then @code{#f} (not the\n"
  558. "empty list) is returned.")
  559. #define FUNC_NAME s_scm_member
  560. {
  561. SCM_VALIDATE_LIST (2, lst);
  562. for (; !SCM_NULL_OR_NIL_P (lst); lst = SCM_CDR (lst))
  563. {
  564. if (! scm_is_false (scm_equal_p (SCM_CAR (lst), x)))
  565. return lst;
  566. }
  567. return SCM_BOOL_F;
  568. }
  569. #undef FUNC_NAME
  570. /* deleting elements from a list (delq, etc.) */
  571. SCM_DEFINE (scm_delq_x, "delq!", 2, 0, 0,
  572. (SCM item, SCM lst),
  573. "@deffnx {Scheme Procedure} delv! item lst\n"
  574. "@deffnx {Scheme Procedure} delete! item lst\n"
  575. "These procedures are destructive versions of @code{delq}, @code{delv}\n"
  576. "and @code{delete}: they modify the existing @var{lst}\n"
  577. "rather than creating a new list. Caveat evaluator: Like other\n"
  578. "destructive list functions, these functions cannot modify the binding of\n"
  579. "@var{lst}, and so cannot be used to delete the first element of\n"
  580. "@var{lst} destructively.")
  581. #define FUNC_NAME s_scm_delq_x
  582. {
  583. SCM walk;
  584. SCM *prev;
  585. for (prev = &lst, walk = lst;
  586. scm_is_pair (walk);
  587. walk = SCM_CDR (walk))
  588. {
  589. if (scm_is_eq (SCM_CAR (walk), item))
  590. *prev = SCM_CDR (walk);
  591. else
  592. prev = SCM_CDRLOC (walk);
  593. }
  594. return lst;
  595. }
  596. #undef FUNC_NAME
  597. SCM_DEFINE (scm_delv_x, "delv!", 2, 0, 0,
  598. (SCM item, SCM lst),
  599. "Destructively remove all elements from @var{lst} that are\n"
  600. "@code{eqv?} to @var{item}.")
  601. #define FUNC_NAME s_scm_delv_x
  602. {
  603. SCM walk;
  604. SCM *prev;
  605. for (prev = &lst, walk = lst;
  606. scm_is_pair (walk);
  607. walk = SCM_CDR (walk))
  608. {
  609. if (! scm_is_false (scm_eqv_p (SCM_CAR (walk), item)))
  610. *prev = SCM_CDR (walk);
  611. else
  612. prev = SCM_CDRLOC (walk);
  613. }
  614. return lst;
  615. }
  616. #undef FUNC_NAME
  617. SCM_DEFINE (scm_delete_x, "delete!", 2, 0, 0,
  618. (SCM item, SCM lst),
  619. "Destructively remove all elements from @var{lst} that are\n"
  620. "@code{equal?} to @var{item}.")
  621. #define FUNC_NAME s_scm_delete_x
  622. {
  623. SCM walk;
  624. SCM *prev;
  625. for (prev = &lst, walk = lst;
  626. scm_is_pair (walk);
  627. walk = SCM_CDR (walk))
  628. {
  629. if (! scm_is_false (scm_equal_p (SCM_CAR (walk), item)))
  630. *prev = SCM_CDR (walk);
  631. else
  632. prev = SCM_CDRLOC (walk);
  633. }
  634. return lst;
  635. }
  636. #undef FUNC_NAME
  637. SCM_DEFINE (scm_delq, "delq", 2, 0, 0,
  638. (SCM item, SCM lst),
  639. "Return a newly-created copy of @var{lst} with elements\n"
  640. "@code{eq?} to @var{item} removed. This procedure mirrors\n"
  641. "@code{memq}: @code{delq} compares elements of @var{lst} against\n"
  642. "@var{item} with @code{eq?}.")
  643. #define FUNC_NAME s_scm_delq
  644. {
  645. SCM copy = scm_list_copy (lst);
  646. return scm_delq_x (item, copy);
  647. }
  648. #undef FUNC_NAME
  649. SCM_DEFINE (scm_delv, "delv", 2, 0, 0,
  650. (SCM item, SCM lst),
  651. "Return a newly-created copy of @var{lst} with elements\n"
  652. "@code{eqv?} to @var{item} removed. This procedure mirrors\n"
  653. "@code{memv}: @code{delv} compares elements of @var{lst} against\n"
  654. "@var{item} with @code{eqv?}.")
  655. #define FUNC_NAME s_scm_delv
  656. {
  657. SCM copy = scm_list_copy (lst);
  658. return scm_delv_x (item, copy);
  659. }
  660. #undef FUNC_NAME
  661. SCM_DEFINE (scm_delete, "delete", 2, 0, 0,
  662. (SCM item, SCM lst),
  663. "Return a newly-created copy of @var{lst} with elements\n"
  664. "@code{equal?} to @var{item} removed. This procedure mirrors\n"
  665. "@code{member}: @code{delete} compares elements of @var{lst}\n"
  666. "against @var{item} with @code{equal?}.")
  667. #define FUNC_NAME s_scm_delete
  668. {
  669. SCM copy = scm_list_copy (lst);
  670. return scm_delete_x (item, copy);
  671. }
  672. #undef FUNC_NAME
  673. SCM_DEFINE (scm_delq1_x, "delq1!", 2, 0, 0,
  674. (SCM item, SCM lst),
  675. "Like @code{delq!}, but only deletes the first occurrence of\n"
  676. "@var{item} from @var{lst}. Tests for equality using\n"
  677. "@code{eq?}. See also @code{delv1!} and @code{delete1!}.")
  678. #define FUNC_NAME s_scm_delq1_x
  679. {
  680. SCM walk;
  681. SCM *prev;
  682. for (prev = &lst, walk = lst;
  683. scm_is_pair (walk);
  684. walk = SCM_CDR (walk))
  685. {
  686. if (scm_is_eq (SCM_CAR (walk), item))
  687. {
  688. *prev = SCM_CDR (walk);
  689. break;
  690. }
  691. else
  692. prev = SCM_CDRLOC (walk);
  693. }
  694. return lst;
  695. }
  696. #undef FUNC_NAME
  697. SCM_DEFINE (scm_delv1_x, "delv1!", 2, 0, 0,
  698. (SCM item, SCM lst),
  699. "Like @code{delv!}, but only deletes the first occurrence of\n"
  700. "@var{item} from @var{lst}. Tests for equality using\n"
  701. "@code{eqv?}. See also @code{delq1!} and @code{delete1!}.")
  702. #define FUNC_NAME s_scm_delv1_x
  703. {
  704. SCM walk;
  705. SCM *prev;
  706. for (prev = &lst, walk = lst;
  707. scm_is_pair (walk);
  708. walk = SCM_CDR (walk))
  709. {
  710. if (! scm_is_false (scm_eqv_p (SCM_CAR (walk), item)))
  711. {
  712. *prev = SCM_CDR (walk);
  713. break;
  714. }
  715. else
  716. prev = SCM_CDRLOC (walk);
  717. }
  718. return lst;
  719. }
  720. #undef FUNC_NAME
  721. SCM_DEFINE (scm_delete1_x, "delete1!", 2, 0, 0,
  722. (SCM item, SCM lst),
  723. "Like @code{delete!}, but only deletes the first occurrence of\n"
  724. "@var{item} from @var{lst}. Tests for equality using\n"
  725. "@code{equal?}. See also @code{delq1!} and @code{delv1!}.")
  726. #define FUNC_NAME s_scm_delete1_x
  727. {
  728. SCM walk;
  729. SCM *prev;
  730. for (prev = &lst, walk = lst;
  731. scm_is_pair (walk);
  732. walk = SCM_CDR (walk))
  733. {
  734. if (! scm_is_false (scm_equal_p (SCM_CAR (walk), item)))
  735. {
  736. *prev = SCM_CDR (walk);
  737. break;
  738. }
  739. else
  740. prev = SCM_CDRLOC (walk);
  741. }
  742. return lst;
  743. }
  744. #undef FUNC_NAME
  745. SCM_DEFINE (scm_filter, "filter", 2, 0, 0,
  746. (SCM pred, SCM list),
  747. "Return all the elements of 2nd arg @var{list} that satisfy predicate @var{pred}.\n"
  748. "The list is not disordered -- elements that appear in the result list occur\n"
  749. "in the same order as they occur in the argument list. The returned list may\n"
  750. "share a common tail with the argument list. The dynamic order in which the\n"
  751. "various applications of pred are made is not specified.\n\n"
  752. "@lisp\n"
  753. "(filter even? '(0 7 8 8 43 -4)) => (0 8 8 -4)\n"
  754. "@end lisp")
  755. #define FUNC_NAME s_scm_filter
  756. {
  757. scm_t_trampoline_1 call = scm_trampoline_1 (pred);
  758. SCM walk;
  759. SCM *prev;
  760. SCM res = SCM_EOL;
  761. SCM_ASSERT (call, pred, 1, FUNC_NAME);
  762. SCM_VALIDATE_LIST (2, list);
  763. for (prev = &res, walk = list;
  764. scm_is_pair (walk);
  765. walk = SCM_CDR (walk))
  766. {
  767. if (scm_is_true (call (pred, SCM_CAR (walk))))
  768. {
  769. *prev = scm_cons (SCM_CAR (walk), SCM_EOL);
  770. prev = SCM_CDRLOC (*prev);
  771. }
  772. }
  773. return res;
  774. }
  775. #undef FUNC_NAME
  776. SCM_DEFINE (scm_filter_x, "filter!", 2, 0, 0,
  777. (SCM pred, SCM list),
  778. "Linear-update variant of @code{filter}.")
  779. #define FUNC_NAME s_scm_filter_x
  780. {
  781. scm_t_trampoline_1 call = scm_trampoline_1 (pred);
  782. SCM walk;
  783. SCM *prev;
  784. SCM_ASSERT (call, pred, 1, FUNC_NAME);
  785. SCM_VALIDATE_LIST (2, list);
  786. for (prev = &list, walk = list;
  787. scm_is_pair (walk);
  788. walk = SCM_CDR (walk))
  789. {
  790. if (scm_is_true (call (pred, SCM_CAR (walk))))
  791. prev = SCM_CDRLOC (walk);
  792. else
  793. *prev = SCM_CDR (walk);
  794. }
  795. return list;
  796. }
  797. #undef FUNC_NAME
  798. void
  799. scm_init_list ()
  800. {
  801. #include "libguile/list.x"
  802. }
  803. /*
  804. Local Variables:
  805. c-file-style: "gnu"
  806. End:
  807. */