list.c 25 KB

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