list.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020
  1. /* Copyright (C) 1995-1997, 2000, 2001, 2003, 2004, 2008-2011,
  2. * 2014 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/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_UNPACK (x), SCM_UNPACK (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_HEAP_OBJECT_P (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. {
  156. if (!scm_is_pair (hare))
  157. return SCM_NULL_OR_NIL_P (hare) ? i : -1;
  158. hare = SCM_CDR (hare);
  159. i++;
  160. if (!scm_is_pair (hare))
  161. return SCM_NULL_OR_NIL_P (hare) ? i : -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 args),
  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. int argnum = 1;
  238. SCM_VALIDATE_REST_ARGUMENT (args);
  239. if (scm_is_null (args))
  240. return SCM_EOL;
  241. loc = &ret;
  242. for (;;)
  243. {
  244. SCM arg = SCM_CAR (args);
  245. *loc = arg;
  246. args = SCM_CDR (args);
  247. if (scm_is_null (args))
  248. return ret;
  249. if (!SCM_NULL_OR_NIL_P (arg))
  250. {
  251. SCM_VALIDATE_CONS (argnum, arg);
  252. loc = SCM_CDRLOC (scm_last_pair (arg));
  253. SCM_VALIDATE_NULL_OR_NIL (argnum, *loc);
  254. }
  255. argnum++;
  256. }
  257. }
  258. #undef FUNC_NAME
  259. SCM_DEFINE (scm_last_pair, "last-pair", 1, 0, 0,
  260. (SCM lst),
  261. "Return the last pair in @var{lst}, signalling an error if\n"
  262. "@var{lst} is circular.")
  263. #define FUNC_NAME s_scm_last_pair
  264. {
  265. SCM tortoise = lst;
  266. SCM hare = lst;
  267. if (SCM_NULL_OR_NIL_P (lst))
  268. return lst;
  269. SCM_VALIDATE_CONS (SCM_ARG1, lst);
  270. do {
  271. SCM ahead = SCM_CDR(hare);
  272. if (!scm_is_pair (ahead)) return hare;
  273. hare = ahead;
  274. ahead = SCM_CDR(hare);
  275. if (!scm_is_pair (ahead)) return hare;
  276. hare = ahead;
  277. tortoise = SCM_CDR(tortoise);
  278. }
  279. while (!scm_is_eq (hare, tortoise));
  280. SCM_MISC_ERROR ("Circular structure in position 1: ~S", scm_list_1 (lst));
  281. }
  282. #undef FUNC_NAME
  283. /* reversing lists */
  284. SCM_DEFINE (scm_reverse, "reverse", 1, 0, 0,
  285. (SCM lst),
  286. "Return a new list that contains the elements of @var{lst} but\n"
  287. "in reverse order.")
  288. #define FUNC_NAME s_scm_reverse
  289. {
  290. SCM result = SCM_EOL;
  291. SCM tortoise = lst;
  292. SCM hare = lst;
  293. do {
  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. if (SCM_NULL_OR_NIL_P(hare)) return result;
  299. SCM_ASSERT(scm_is_pair(hare), lst, 1, FUNC_NAME);
  300. result = scm_cons (SCM_CAR (hare), result);
  301. hare = SCM_CDR (hare);
  302. tortoise = SCM_CDR (tortoise);
  303. }
  304. while (!scm_is_eq (hare, tortoise));
  305. SCM_MISC_ERROR ("Circular structure in position 1: ~S", scm_list_1 (lst));
  306. }
  307. #undef FUNC_NAME
  308. SCM_DEFINE (scm_reverse_x, "reverse!", 1, 1, 0,
  309. (SCM lst, SCM new_tail),
  310. "A destructive version of @code{reverse} (@pxref{Pairs and Lists,,,r5rs,\n"
  311. "The Revised^5 Report on Scheme}). The cdr of each cell in @var{lst} is\n"
  312. "modified to point to the previous list element. Return the\n"
  313. "reversed list.\n\n"
  314. "Caveat: because the list is modified in place, the tail of the original\n"
  315. "list now becomes its head, and the head of the original list now becomes\n"
  316. "the tail. Therefore, the @var{lst} symbol to which the head of the\n"
  317. "original list was bound now points to the tail. To ensure that the head\n"
  318. "of the modified list is not lost, it is wise to save the return value of\n"
  319. "@code{reverse!}")
  320. #define FUNC_NAME s_scm_reverse_x
  321. {
  322. SCM old_lst = lst;
  323. SCM tail = SCM_BOOL_F;
  324. if (SCM_UNBNDP (new_tail))
  325. new_tail = SCM_EOL;
  326. if (SCM_NULL_OR_NIL_P (lst))
  327. return new_tail;
  328. /* SCM_VALIDATE_LIST would run through the whole list to make sure it
  329. is not eventually circular. In contrast to most list operations,
  330. reverse! cannot get stuck in an infinite loop but arrives back at
  331. the start when given an eventually or fully circular list. Because
  332. of that, we can save the cost of an upfront proper list check at
  333. the price of having to do a double reversal in the error case.
  334. */
  335. while (scm_is_pair (lst))
  336. {
  337. SCM old_tail = SCM_CDR (lst);
  338. SCM_SETCDR (lst, tail);
  339. tail = lst;
  340. lst = old_tail;
  341. }
  342. if (SCM_LIKELY (SCM_NULL_OR_NIL_P (lst)))
  343. {
  344. SCM_SETCDR (old_lst, new_tail);
  345. return tail;
  346. }
  347. /* We did not start with a proper list. Undo the reversal. */
  348. while (scm_is_pair (tail))
  349. {
  350. SCM old_tail = SCM_CDR (tail);
  351. SCM_SETCDR (tail, lst);
  352. lst = tail;
  353. tail = old_tail;
  354. }
  355. SCM_WRONG_TYPE_ARG (1, lst);
  356. return lst;
  357. }
  358. #undef FUNC_NAME
  359. /* indexing lists by element number */
  360. SCM_DEFINE (scm_list_ref, "list-ref", 2, 0, 0,
  361. (SCM list, SCM k),
  362. "Return the @var{k}th element from @var{list}.")
  363. #define FUNC_NAME s_scm_list_ref
  364. {
  365. SCM lst = list;
  366. unsigned long int i;
  367. i = scm_to_ulong (k);
  368. while (scm_is_pair (lst)) {
  369. if (i == 0)
  370. return SCM_CAR (lst);
  371. else {
  372. --i;
  373. lst = SCM_CDR (lst);
  374. }
  375. };
  376. if (SCM_NULL_OR_NIL_P (lst))
  377. SCM_OUT_OF_RANGE (2, k);
  378. else
  379. SCM_WRONG_TYPE_ARG (1, list);
  380. }
  381. #undef FUNC_NAME
  382. SCM_DEFINE (scm_list_set_x, "list-set!", 3, 0, 0,
  383. (SCM list, SCM k, SCM val),
  384. "Set the @var{k}th element of @var{list} to @var{val}.")
  385. #define FUNC_NAME s_scm_list_set_x
  386. {
  387. SCM lst = list;
  388. unsigned long int i = scm_to_ulong (k);
  389. while (scm_is_pair (lst)) {
  390. if (i == 0) {
  391. SCM_SETCAR (lst, val);
  392. return val;
  393. } else {
  394. --i;
  395. lst = SCM_CDR (lst);
  396. }
  397. };
  398. if (SCM_NULL_OR_NIL_P (lst))
  399. SCM_OUT_OF_RANGE (2, k);
  400. else
  401. SCM_WRONG_TYPE_ARG (1, list);
  402. }
  403. #undef FUNC_NAME
  404. SCM_REGISTER_PROC(s_list_cdr_ref, "list-cdr-ref", 2, 0, 0, scm_list_tail);
  405. SCM_DEFINE (scm_list_tail, "list-tail", 2, 0, 0,
  406. (SCM lst, SCM k),
  407. "@deffnx {Scheme Procedure} list-cdr-ref lst k\n"
  408. "Return the \"tail\" of @var{lst} beginning with its @var{k}th element.\n"
  409. "The first element of the list is considered to be element 0.\n\n"
  410. "@code{list-tail} and @code{list-cdr-ref} are identical. It may help to\n"
  411. "think of @code{list-cdr-ref} as accessing the @var{k}th cdr of the list,\n"
  412. "or returning the results of cdring @var{k} times down @var{lst}.")
  413. #define FUNC_NAME s_scm_list_tail
  414. {
  415. size_t i = scm_to_size_t (k);
  416. while (i-- > 0) {
  417. SCM_VALIDATE_CONS (1, lst);
  418. lst = SCM_CDR(lst);
  419. }
  420. return lst;
  421. }
  422. #undef FUNC_NAME
  423. SCM_DEFINE (scm_list_cdr_set_x, "list-cdr-set!", 3, 0, 0,
  424. (SCM list, SCM k, SCM val),
  425. "Set the @var{k}th cdr of @var{list} to @var{val}.")
  426. #define FUNC_NAME s_scm_list_cdr_set_x
  427. {
  428. SCM lst = list;
  429. size_t i = scm_to_size_t (k);
  430. while (scm_is_pair (lst)) {
  431. if (i == 0) {
  432. SCM_SETCDR (lst, val);
  433. return val;
  434. } else {
  435. --i;
  436. lst = SCM_CDR (lst);
  437. }
  438. };
  439. if (SCM_NULL_OR_NIL_P (lst))
  440. SCM_OUT_OF_RANGE (2, k);
  441. else
  442. SCM_WRONG_TYPE_ARG (1, list);
  443. }
  444. #undef FUNC_NAME
  445. /* copying lists, perhaps partially */
  446. SCM_DEFINE (scm_list_head, "list-head", 2, 0, 0,
  447. (SCM lst, SCM k),
  448. "Copy the first @var{k} elements from @var{lst} into a new list, and\n"
  449. "return it.")
  450. #define FUNC_NAME s_scm_list_head
  451. {
  452. SCM answer;
  453. SCM * pos;
  454. size_t i = scm_to_size_t (k);
  455. answer = SCM_EOL;
  456. pos = &answer;
  457. while (i-- > 0)
  458. {
  459. SCM_VALIDATE_CONS (1, lst);
  460. *pos = scm_cons (SCM_CAR (lst), SCM_EOL);
  461. pos = SCM_CDRLOC (*pos);
  462. lst = SCM_CDR(lst);
  463. }
  464. return answer;
  465. }
  466. #undef FUNC_NAME
  467. /* Copy a list which is known to be finite. The last pair may or may not have
  468. * a '() in its cdr. That is, improper lists are accepted. */
  469. SCM
  470. scm_i_finite_list_copy (SCM list)
  471. {
  472. if (!scm_is_pair (list))
  473. {
  474. return list;
  475. }
  476. else
  477. {
  478. SCM tail;
  479. const SCM result = tail = scm_list_1 (SCM_CAR (list));
  480. list = SCM_CDR (list);
  481. while (scm_is_pair (list))
  482. {
  483. const SCM new_tail = scm_list_1 (SCM_CAR (list));
  484. SCM_SETCDR (tail, new_tail);
  485. tail = new_tail;
  486. list = SCM_CDR (list);
  487. }
  488. SCM_SETCDR (tail, list);
  489. return result;
  490. }
  491. }
  492. SCM_DEFINE (scm_list_copy, "list-copy", 1, 0, 0,
  493. (SCM lst),
  494. "Return a (newly-created) copy of @var{lst}.")
  495. #define FUNC_NAME s_scm_list_copy
  496. {
  497. SCM newlst;
  498. SCM * fill_here;
  499. SCM from_here;
  500. SCM_VALIDATE_LIST (1, lst);
  501. newlst = SCM_EOL;
  502. fill_here = &newlst;
  503. from_here = lst;
  504. while (scm_is_pair (from_here))
  505. {
  506. SCM c;
  507. c = scm_cons (SCM_CAR (from_here), SCM_CDR (from_here));
  508. *fill_here = c;
  509. fill_here = SCM_CDRLOC (c);
  510. from_here = SCM_CDR (from_here);
  511. }
  512. return newlst;
  513. }
  514. #undef FUNC_NAME
  515. SCM_PROC (s_list, "list", 0, 0, 1, scm_list_copy);
  516. SCM_SNARF_DOCS (primitive, scm_list_copy, "list", (SCM objs), 0, 0, 1,
  517. "Return a list containing @var{objs}, the arguments to\n"
  518. "@code{list}.")
  519. /* This used to be the code for "list", but it's wrong when used via apply
  520. (it should copy the list). It seems pretty unlikely anyone would have
  521. been using this from C code, since it's a no-op, but keep it for strict
  522. binary compatibility. */
  523. SCM
  524. scm_list (SCM objs)
  525. {
  526. return objs;
  527. }
  528. /* membership tests (memq, memv, etc.) */
  529. /* The function scm_c_memq returns the first sublist of list whose car is
  530. * 'eq?' obj, where the sublists of list are the non-empty lists returned by
  531. * (list-tail list k) for k less than the length of list. If obj does not
  532. * occur in list, then #f (not the empty list) is returned.
  533. * List must be a proper list, otherwise scm_c_memq may crash or loop
  534. * endlessly.
  535. */
  536. SCM
  537. scm_c_memq (SCM obj, SCM list)
  538. {
  539. for (; !SCM_NULL_OR_NIL_P (list); list = SCM_CDR (list))
  540. {
  541. if (scm_is_eq (SCM_CAR (list), obj))
  542. return list;
  543. }
  544. return SCM_BOOL_F;
  545. }
  546. SCM_DEFINE (scm_memq, "memq", 2, 0, 0,
  547. (SCM x, SCM lst),
  548. "Return the first sublist of @var{lst} whose car is @code{eq?}\n"
  549. "to @var{x} where the sublists of @var{lst} are the non-empty\n"
  550. "lists returned by @code{(list-tail @var{lst} @var{k})} for\n"
  551. "@var{k} less than the length of @var{lst}. If @var{x} does not\n"
  552. "occur in @var{lst}, then @code{#f} (not the empty list) is\n"
  553. "returned.")
  554. #define FUNC_NAME s_scm_memq
  555. {
  556. SCM hare = lst, tortoise = lst;
  557. while (scm_is_pair (hare))
  558. {
  559. if (scm_is_eq (SCM_CAR (hare), x))
  560. return hare;
  561. else
  562. hare = SCM_CDR (hare);
  563. if (!scm_is_pair (hare))
  564. break;
  565. if (scm_is_eq (SCM_CAR (hare), x))
  566. return hare;
  567. else
  568. hare = SCM_CDR (hare);
  569. tortoise = SCM_CDR (tortoise);
  570. if (SCM_UNLIKELY (scm_is_eq (hare, tortoise)))
  571. break;
  572. }
  573. if (SCM_LIKELY (scm_is_null (hare)))
  574. return SCM_BOOL_F;
  575. else
  576. scm_wrong_type_arg_msg (FUNC_NAME, 2, lst, "list");
  577. }
  578. #undef FUNC_NAME
  579. SCM_DEFINE (scm_memv, "memv", 2, 0, 0,
  580. (SCM x, SCM lst),
  581. "Return the first sublist of @var{lst} whose car is @code{eqv?}\n"
  582. "to @var{x} where the sublists of @var{lst} are the non-empty\n"
  583. "lists returned by @code{(list-tail @var{lst} @var{k})} for\n"
  584. "@var{k} less than the length of @var{lst}. If @var{x} does not\n"
  585. "occur in @var{lst}, then @code{#f} (not the empty list) is\n"
  586. "returned.")
  587. #define FUNC_NAME s_scm_memv
  588. {
  589. SCM hare = lst, tortoise = lst;
  590. while (scm_is_pair (hare))
  591. {
  592. if (scm_is_true (scm_eqv_p (SCM_CAR (hare), x)))
  593. return hare;
  594. else
  595. hare = SCM_CDR (hare);
  596. if (!scm_is_pair (hare))
  597. break;
  598. if (scm_is_true (scm_eqv_p (SCM_CAR (hare), x)))
  599. return hare;
  600. else
  601. hare = SCM_CDR (hare);
  602. tortoise = SCM_CDR (tortoise);
  603. if (SCM_UNLIKELY (scm_is_eq (hare, tortoise)))
  604. break;
  605. }
  606. if (SCM_LIKELY (scm_is_null (hare)))
  607. return SCM_BOOL_F;
  608. else
  609. scm_wrong_type_arg_msg (FUNC_NAME, 2, lst, "list");
  610. }
  611. #undef FUNC_NAME
  612. SCM_DEFINE (scm_member, "member", 2, 0, 0,
  613. (SCM x, SCM lst),
  614. "Return the first sublist of @var{lst} whose car is\n"
  615. "@code{equal?} to @var{x} where the sublists of @var{lst} are\n"
  616. "the non-empty lists returned by @code{(list-tail @var{lst}\n"
  617. "@var{k})} for @var{k} less than the length of @var{lst}. If\n"
  618. "@var{x} does not occur in @var{lst}, then @code{#f} (not the\n"
  619. "empty list) is returned.")
  620. #define FUNC_NAME s_scm_member
  621. {
  622. SCM_VALIDATE_LIST (2, lst);
  623. for (; !SCM_NULL_OR_NIL_P (lst); lst = SCM_CDR (lst))
  624. {
  625. if (! scm_is_false (scm_equal_p (SCM_CAR (lst), x)))
  626. return lst;
  627. }
  628. return SCM_BOOL_F;
  629. }
  630. #undef FUNC_NAME
  631. /* deleting elements from a list (delq, etc.) */
  632. SCM_DEFINE (scm_delq_x, "delq!", 2, 0, 0,
  633. (SCM item, SCM lst),
  634. "@deffnx {Scheme Procedure} delv! item lst\n"
  635. "@deffnx {Scheme Procedure} delete! item lst\n"
  636. "These procedures are destructive versions of @code{delq}, @code{delv}\n"
  637. "and @code{delete}: they modify the existing @var{lst}\n"
  638. "rather than creating a new list. Caveat evaluator: Like other\n"
  639. "destructive list functions, these functions cannot modify the binding of\n"
  640. "@var{lst}, and so cannot be used to delete the first element of\n"
  641. "@var{lst} destructively.")
  642. #define FUNC_NAME s_scm_delq_x
  643. {
  644. SCM walk;
  645. SCM *prev;
  646. for (prev = &lst, walk = lst;
  647. scm_is_pair (walk);
  648. walk = SCM_CDR (walk))
  649. {
  650. if (scm_is_eq (SCM_CAR (walk), item))
  651. *prev = SCM_CDR (walk);
  652. else
  653. prev = SCM_CDRLOC (walk);
  654. }
  655. return lst;
  656. }
  657. #undef FUNC_NAME
  658. SCM_DEFINE (scm_delv_x, "delv!", 2, 0, 0,
  659. (SCM item, SCM lst),
  660. "Destructively remove all elements from @var{lst} that are\n"
  661. "@code{eqv?} to @var{item}.")
  662. #define FUNC_NAME s_scm_delv_x
  663. {
  664. SCM walk;
  665. SCM *prev;
  666. for (prev = &lst, walk = lst;
  667. scm_is_pair (walk);
  668. walk = SCM_CDR (walk))
  669. {
  670. if (! scm_is_false (scm_eqv_p (SCM_CAR (walk), item)))
  671. *prev = SCM_CDR (walk);
  672. else
  673. prev = SCM_CDRLOC (walk);
  674. }
  675. return lst;
  676. }
  677. #undef FUNC_NAME
  678. SCM_DEFINE (scm_delete_x, "delete!", 2, 0, 0,
  679. (SCM item, SCM lst),
  680. "Destructively remove all elements from @var{lst} that are\n"
  681. "@code{equal?} to @var{item}.")
  682. #define FUNC_NAME s_scm_delete_x
  683. {
  684. SCM walk;
  685. SCM *prev;
  686. for (prev = &lst, walk = lst;
  687. scm_is_pair (walk);
  688. walk = SCM_CDR (walk))
  689. {
  690. if (! scm_is_false (scm_equal_p (SCM_CAR (walk), item)))
  691. *prev = SCM_CDR (walk);
  692. else
  693. prev = SCM_CDRLOC (walk);
  694. }
  695. return lst;
  696. }
  697. #undef FUNC_NAME
  698. SCM_DEFINE (scm_delq, "delq", 2, 0, 0,
  699. (SCM item, SCM lst),
  700. "Return a newly-created copy of @var{lst} with elements\n"
  701. "@code{eq?} to @var{item} removed. This procedure mirrors\n"
  702. "@code{memq}: @code{delq} compares elements of @var{lst} against\n"
  703. "@var{item} with @code{eq?}.")
  704. #define FUNC_NAME s_scm_delq
  705. {
  706. SCM copy = scm_list_copy (lst);
  707. return scm_delq_x (item, copy);
  708. }
  709. #undef FUNC_NAME
  710. SCM_DEFINE (scm_delv, "delv", 2, 0, 0,
  711. (SCM item, SCM lst),
  712. "Return a newly-created copy of @var{lst} with elements\n"
  713. "@code{eqv?} to @var{item} removed. This procedure mirrors\n"
  714. "@code{memv}: @code{delv} compares elements of @var{lst} against\n"
  715. "@var{item} with @code{eqv?}.")
  716. #define FUNC_NAME s_scm_delv
  717. {
  718. SCM copy = scm_list_copy (lst);
  719. return scm_delv_x (item, copy);
  720. }
  721. #undef FUNC_NAME
  722. SCM_DEFINE (scm_delete, "delete", 2, 0, 0,
  723. (SCM item, SCM lst),
  724. "Return a newly-created copy of @var{lst} with elements\n"
  725. "@code{equal?} to @var{item} removed. This procedure mirrors\n"
  726. "@code{member}: @code{delete} compares elements of @var{lst}\n"
  727. "against @var{item} with @code{equal?}.")
  728. #define FUNC_NAME s_scm_delete
  729. {
  730. SCM copy = scm_list_copy (lst);
  731. return scm_delete_x (item, copy);
  732. }
  733. #undef FUNC_NAME
  734. SCM_DEFINE (scm_delq1_x, "delq1!", 2, 0, 0,
  735. (SCM item, SCM lst),
  736. "Like @code{delq!}, but only deletes the first occurrence of\n"
  737. "@var{item} from @var{lst}. Tests for equality using\n"
  738. "@code{eq?}. See also @code{delv1!} and @code{delete1!}.")
  739. #define FUNC_NAME s_scm_delq1_x
  740. {
  741. SCM walk;
  742. SCM *prev;
  743. for (prev = &lst, walk = lst;
  744. scm_is_pair (walk);
  745. walk = SCM_CDR (walk))
  746. {
  747. if (scm_is_eq (SCM_CAR (walk), item))
  748. {
  749. *prev = SCM_CDR (walk);
  750. break;
  751. }
  752. else
  753. prev = SCM_CDRLOC (walk);
  754. }
  755. return lst;
  756. }
  757. #undef FUNC_NAME
  758. SCM_DEFINE (scm_delv1_x, "delv1!", 2, 0, 0,
  759. (SCM item, SCM lst),
  760. "Like @code{delv!}, but only deletes the first occurrence of\n"
  761. "@var{item} from @var{lst}. Tests for equality using\n"
  762. "@code{eqv?}. See also @code{delq1!} and @code{delete1!}.")
  763. #define FUNC_NAME s_scm_delv1_x
  764. {
  765. SCM walk;
  766. SCM *prev;
  767. for (prev = &lst, walk = lst;
  768. scm_is_pair (walk);
  769. walk = SCM_CDR (walk))
  770. {
  771. if (! scm_is_false (scm_eqv_p (SCM_CAR (walk), item)))
  772. {
  773. *prev = SCM_CDR (walk);
  774. break;
  775. }
  776. else
  777. prev = SCM_CDRLOC (walk);
  778. }
  779. return lst;
  780. }
  781. #undef FUNC_NAME
  782. SCM_DEFINE (scm_delete1_x, "delete1!", 2, 0, 0,
  783. (SCM item, SCM lst),
  784. "Like @code{delete!}, but only deletes the first occurrence of\n"
  785. "@var{item} from @var{lst}. Tests for equality using\n"
  786. "@code{equal?}. See also @code{delq1!} and @code{delv1!}.")
  787. #define FUNC_NAME s_scm_delete1_x
  788. {
  789. SCM walk;
  790. SCM *prev;
  791. for (prev = &lst, walk = lst;
  792. scm_is_pair (walk);
  793. walk = SCM_CDR (walk))
  794. {
  795. if (! scm_is_false (scm_equal_p (SCM_CAR (walk), item)))
  796. {
  797. *prev = SCM_CDR (walk);
  798. break;
  799. }
  800. else
  801. prev = SCM_CDRLOC (walk);
  802. }
  803. return lst;
  804. }
  805. #undef FUNC_NAME
  806. SCM_DEFINE (scm_filter, "filter", 2, 0, 0,
  807. (SCM pred, SCM list),
  808. "Return all the elements of 2nd arg @var{list} that satisfy predicate @var{pred}.\n"
  809. "The list is not disordered -- elements that appear in the result list occur\n"
  810. "in the same order as they occur in the argument list. The returned list may\n"
  811. "share a common tail with the argument list. The dynamic order in which the\n"
  812. "various applications of pred are made is not specified.\n\n"
  813. "@lisp\n"
  814. "(filter even? '(0 7 8 8 43 -4)) => (0 8 8 -4)\n"
  815. "@end lisp")
  816. #define FUNC_NAME s_scm_filter
  817. {
  818. SCM walk;
  819. SCM *prev;
  820. SCM res = SCM_EOL;
  821. SCM_ASSERT (scm_is_true (scm_procedure_p (pred)), pred, 1, FUNC_NAME);
  822. SCM_VALIDATE_LIST (2, list);
  823. for (prev = &res, walk = list;
  824. scm_is_pair (walk);
  825. walk = SCM_CDR (walk))
  826. {
  827. if (scm_is_true (scm_call_1 (pred, SCM_CAR (walk))))
  828. {
  829. *prev = scm_cons (SCM_CAR (walk), SCM_EOL);
  830. prev = SCM_CDRLOC (*prev);
  831. }
  832. }
  833. return res;
  834. }
  835. #undef FUNC_NAME
  836. SCM_DEFINE (scm_filter_x, "filter!", 2, 0, 0,
  837. (SCM pred, SCM list),
  838. "Linear-update variant of @code{filter}.")
  839. #define FUNC_NAME s_scm_filter_x
  840. {
  841. SCM walk;
  842. SCM *prev;
  843. SCM_ASSERT (scm_is_true (scm_procedure_p (pred)), pred, 1, FUNC_NAME);
  844. SCM_VALIDATE_LIST (2, list);
  845. for (prev = &list, walk = list;
  846. scm_is_pair (walk);
  847. walk = SCM_CDR (walk))
  848. {
  849. if (scm_is_true (scm_call_1 (pred, SCM_CAR (walk))))
  850. prev = SCM_CDRLOC (walk);
  851. else
  852. *prev = SCM_CDR (walk);
  853. }
  854. return list;
  855. }
  856. #undef FUNC_NAME
  857. void
  858. scm_init_list ()
  859. {
  860. #include "libguile/list.x"
  861. }
  862. /*
  863. Local Variables:
  864. c-file-style: "gnu"
  865. End:
  866. */