list.c 23 KB

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