srfi-1.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955
  1. /* srfi-1.c --- SRFI-1 procedures for Guile
  2. *
  3. * Copyright (C) 1995-1997, 2000-2003, 2005, 2006, 2008-2011, 2013
  4. * 2014 Free Software Foundation, Inc.
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public License
  8. * as published by the Free Software Foundation; either version 3 of
  9. * the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19. * 02110-1301 USA
  20. */
  21. #ifdef HAVE_CONFIG_H
  22. # include <config.h>
  23. #endif
  24. #include "libguile/_scm.h"
  25. #include "libguile/eq.h"
  26. #include "libguile/validate.h"
  27. #include "libguile/list.h"
  28. #include "libguile/eval.h"
  29. #include "libguile/srfi-1.h"
  30. #include <stdarg.h>
  31. /* The intent of this file was to gradually replace those Scheme
  32. * procedures in srfi-1.scm that extend core primitive procedures,
  33. * so that using srfi-1 wouldn't have performance penalties.
  34. *
  35. * However, we now prefer to write these procedures in Scheme, let the compiler
  36. * optimize them, and have the VM execute them efficiently.
  37. */
  38. static SCM
  39. equal_trampoline (SCM proc, SCM arg1, SCM arg2)
  40. {
  41. return scm_equal_p (arg1, arg2);
  42. }
  43. /* list_copy_part() copies the first COUNT cells of LST, puts the result at
  44. *dst, and returns the SCM_CDRLOC of the last cell in that new list.
  45. This function is designed to be careful about LST possibly having changed
  46. in between the caller deciding what to copy, and the copy actually being
  47. done here. The COUNT ensures we terminate if LST has become circular,
  48. SCM_VALIDATE_CONS guards against a cdr in the list changed to some
  49. non-pair object. */
  50. #include <stdio.h>
  51. static SCM *
  52. list_copy_part (SCM lst, int count, SCM *dst)
  53. #define FUNC_NAME "list_copy_part"
  54. {
  55. SCM c;
  56. for ( ; count > 0; count--)
  57. {
  58. SCM_VALIDATE_CONS (SCM_ARGn, lst);
  59. c = scm_cons (SCM_CAR (lst), SCM_EOL);
  60. *dst = c;
  61. dst = SCM_CDRLOC (c);
  62. lst = SCM_CDR (lst);
  63. }
  64. return dst;
  65. }
  66. #undef FUNC_NAME
  67. SCM_DEFINE (scm_srfi1_append_reverse, "append-reverse", 2, 0, 0,
  68. (SCM revhead, SCM tail),
  69. "Reverse @var{rev-head}, append @var{tail} to it, and return the\n"
  70. "result. This is equivalent to @code{(append (reverse\n"
  71. "@var{rev-head}) @var{tail})}, but its implementation is more\n"
  72. "efficient.\n"
  73. "\n"
  74. "@example\n"
  75. "(append-reverse '(1 2 3) '(4 5 6)) @result{} (3 2 1 4 5 6)\n"
  76. "@end example")
  77. #define FUNC_NAME s_scm_srfi1_append_reverse
  78. {
  79. while (scm_is_pair (revhead))
  80. {
  81. /* copy first element of revhead onto front of tail */
  82. tail = scm_cons (SCM_CAR (revhead), tail);
  83. revhead = SCM_CDR (revhead);
  84. }
  85. SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (revhead), revhead, SCM_ARG1, FUNC_NAME,
  86. "list");
  87. return tail;
  88. }
  89. #undef FUNC_NAME
  90. SCM_DEFINE (scm_srfi1_append_reverse_x, "append-reverse!", 2, 0, 0,
  91. (SCM revhead, SCM tail),
  92. "Reverse @var{rev-head}, append @var{tail} to it, and return the\n"
  93. "result. This is equivalent to @code{(append! (reverse!\n"
  94. "@var{rev-head}) @var{tail})}, but its implementation is more\n"
  95. "efficient.\n"
  96. "\n"
  97. "@example\n"
  98. "(append-reverse! (list 1 2 3) '(4 5 6)) @result{} (3 2 1 4 5 6)\n"
  99. "@end example\n"
  100. "\n"
  101. "@var{rev-head} may be modified in order to produce the result.")
  102. #define FUNC_NAME s_scm_srfi1_append_reverse_x
  103. {
  104. SCM newtail;
  105. while (scm_is_pair (revhead))
  106. {
  107. /* take the first cons cell from revhead */
  108. newtail = revhead;
  109. revhead = SCM_CDR (revhead);
  110. /* make it the new start of tail, appending the previous */
  111. SCM_SETCDR (newtail, tail);
  112. tail = newtail;
  113. }
  114. SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (revhead), revhead, SCM_ARG1, FUNC_NAME,
  115. "list");
  116. return tail;
  117. }
  118. #undef FUNC_NAME
  119. SCM_DEFINE (scm_srfi1_concatenate, "concatenate", 1, 0, 0,
  120. (SCM lstlst),
  121. "Construct a list by appending all lists in @var{lstlst}.\n"
  122. "\n"
  123. "@code{concatenate} is the same as @code{(apply append\n"
  124. "@var{lstlst})}. It exists because some Scheme implementations\n"
  125. "have a limit on the number of arguments a function takes, which\n"
  126. "the @code{apply} might exceed. In Guile there is no such\n"
  127. "limit.")
  128. #define FUNC_NAME s_scm_srfi1_concatenate
  129. {
  130. SCM_VALIDATE_LIST (SCM_ARG1, lstlst);
  131. return scm_append (lstlst);
  132. }
  133. #undef FUNC_NAME
  134. SCM_DEFINE (scm_srfi1_concatenate_x, "concatenate!", 1, 0, 0,
  135. (SCM lstlst),
  136. "Construct a list by appending all lists in @var{lstlst}. Those\n"
  137. "lists may be modified to produce the result.\n"
  138. "\n"
  139. "@code{concatenate!} is the same as @code{(apply append!\n"
  140. "@var{lstlst})}. It exists because some Scheme implementations\n"
  141. "have a limit on the number of arguments a function takes, which\n"
  142. "the @code{apply} might exceed. In Guile there is no such\n"
  143. "limit.")
  144. #define FUNC_NAME s_scm_srfi1_concatenate_x
  145. {
  146. SCM_VALIDATE_LIST (SCM_ARG1, lstlst);
  147. return scm_append_x (lstlst);
  148. }
  149. #undef FUNC_NAME
  150. SCM_DEFINE (scm_srfi1_count, "count", 2, 0, 1,
  151. (SCM pred, SCM list1, SCM rest),
  152. "Return a count of the number of times @var{pred} returns true\n"
  153. "when called on elements from the given lists.\n"
  154. "\n"
  155. "@var{pred} is called with @var{N} parameters @code{(@var{pred}\n"
  156. "@var{elem1} @dots{} @var{elemN})}, each element being from the\n"
  157. "corresponding @var{list1} @dots{} @var{lstN}. The first call is\n"
  158. "with the first element of each list, the second with the second\n"
  159. "element from each, and so on.\n"
  160. "\n"
  161. "Counting stops when the end of the shortest list is reached.\n"
  162. "At least one list must be non-circular.")
  163. #define FUNC_NAME s_scm_srfi1_count
  164. {
  165. long count;
  166. SCM lst;
  167. int argnum;
  168. SCM_VALIDATE_REST_ARGUMENT (rest);
  169. count = 0;
  170. if (scm_is_null (rest))
  171. {
  172. /* one list */
  173. SCM_ASSERT (scm_is_true (scm_procedure_p (pred)), pred, SCM_ARG1, FUNC_NAME);
  174. for ( ; scm_is_pair (list1); list1 = SCM_CDR (list1))
  175. count += scm_is_true (scm_call_1 (pred, SCM_CAR (list1)));
  176. /* check below that list1 is a proper list, and done */
  177. end_list1:
  178. lst = list1;
  179. argnum = 2;
  180. }
  181. else if (scm_is_pair (rest) && scm_is_null (SCM_CDR (rest)))
  182. {
  183. /* two lists */
  184. SCM list2;
  185. SCM_ASSERT (scm_is_true (scm_procedure_p (pred)), pred, SCM_ARG1, FUNC_NAME);
  186. list2 = SCM_CAR (rest);
  187. for (;;)
  188. {
  189. if (! scm_is_pair (list1))
  190. goto end_list1;
  191. if (! scm_is_pair (list2))
  192. {
  193. lst = list2;
  194. argnum = 3;
  195. break;
  196. }
  197. count += scm_is_true (scm_call_2
  198. (pred, SCM_CAR (list1), SCM_CAR (list2)));
  199. list1 = SCM_CDR (list1);
  200. list2 = SCM_CDR (list2);
  201. }
  202. }
  203. else
  204. {
  205. /* three or more lists */
  206. SCM vec, args, a;
  207. size_t len, i;
  208. /* vec is the list arguments */
  209. vec = scm_vector (scm_cons (list1, rest));
  210. len = SCM_SIMPLE_VECTOR_LENGTH (vec);
  211. /* args is the argument list to pass to pred, same length as vec,
  212. re-used for each call */
  213. args = scm_make_list (SCM_I_MAKINUM (len), SCM_UNDEFINED);
  214. for (;;)
  215. {
  216. /* first elem of each list in vec into args, and step those
  217. vec entries onto their next element */
  218. for (i = 0, a = args, argnum = 2;
  219. i < len;
  220. i++, a = SCM_CDR (a), argnum++)
  221. {
  222. lst = SCM_SIMPLE_VECTOR_REF (vec, i); /* list argument */
  223. if (! scm_is_pair (lst))
  224. goto check_lst_and_done;
  225. SCM_SETCAR (a, SCM_CAR (lst)); /* arg for pred */
  226. SCM_SIMPLE_VECTOR_SET (vec, i, SCM_CDR (lst)); /* rest of lst */
  227. }
  228. count += scm_is_true (scm_apply_0 (pred, args));
  229. }
  230. }
  231. check_lst_and_done:
  232. SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst), lst, argnum, FUNC_NAME, "list");
  233. return scm_from_long (count);
  234. }
  235. #undef FUNC_NAME
  236. SCM_DEFINE (scm_srfi1_delete, "delete", 2, 1, 0,
  237. (SCM x, SCM lst, SCM pred),
  238. "Return a list containing the elements of @var{lst} but with\n"
  239. "those equal to @var{x} deleted. The returned elements will be\n"
  240. "in the same order as they were in @var{lst}.\n"
  241. "\n"
  242. "Equality is determined by @var{pred}, or @code{equal?} if not\n"
  243. "given. An equality call is made just once for each element,\n"
  244. "but the order in which the calls are made on the elements is\n"
  245. "unspecified.\n"
  246. "\n"
  247. "The equality calls are always @code{(pred x elem)}, ie.@: the\n"
  248. "given @var{x} is first. This means for instance elements\n"
  249. "greater than 5 can be deleted with @code{(delete 5 lst <)}.\n"
  250. "\n"
  251. "@var{lst} is not modified, but the returned list might share a\n"
  252. "common tail with @var{lst}.")
  253. #define FUNC_NAME s_scm_srfi1_delete
  254. {
  255. SCM ret, *p, keeplst;
  256. int count;
  257. if (SCM_UNBNDP (pred))
  258. return scm_delete (x, lst);
  259. SCM_ASSERT (scm_is_true (scm_procedure_p (pred)), pred, SCM_ARG3, FUNC_NAME);
  260. /* ret is the return list being constructed. p is where to append to it,
  261. initially &ret then SCM_CDRLOC of the last pair. lst progresses as
  262. elements are considered.
  263. Elements to be retained are not immediately copied, instead keeplst is
  264. the last pair in lst which is to be retained but not yet copied, count
  265. is how many from there are wanted. When there's no more deletions, *p
  266. can be set to keeplst to share the remainder of the original lst. (The
  267. entire original lst if there's no deletions at all.) */
  268. keeplst = lst;
  269. count = 0;
  270. p = &ret;
  271. for ( ; scm_is_pair (lst); lst = SCM_CDR (lst))
  272. {
  273. if (scm_is_true (scm_call_2 (pred, x, SCM_CAR (lst))))
  274. {
  275. /* delete this element, so copy those at keeplst */
  276. p = list_copy_part (keeplst, count, p);
  277. keeplst = SCM_CDR (lst);
  278. count = 0;
  279. }
  280. else
  281. {
  282. /* keep this element */
  283. count++;
  284. }
  285. }
  286. /* final retained elements */
  287. *p = keeplst;
  288. /* demand that lst was a proper list */
  289. SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst), lst, SCM_ARG2, FUNC_NAME, "list");
  290. return ret;
  291. }
  292. #undef FUNC_NAME
  293. SCM_DEFINE (scm_srfi1_delete_x, "delete!", 2, 1, 0,
  294. (SCM x, SCM lst, SCM pred),
  295. "Return a list containing the elements of @var{lst} but with\n"
  296. "those equal to @var{x} deleted. The returned elements will be\n"
  297. "in the same order as they were in @var{lst}.\n"
  298. "\n"
  299. "Equality is determined by @var{pred}, or @code{equal?} if not\n"
  300. "given. An equality call is made just once for each element,\n"
  301. "but the order in which the calls are made on the elements is\n"
  302. "unspecified.\n"
  303. "\n"
  304. "The equality calls are always @code{(pred x elem)}, ie.@: the\n"
  305. "given @var{x} is first. This means for instance elements\n"
  306. "greater than 5 can be deleted with @code{(delete 5 lst <)}.\n"
  307. "\n"
  308. "@var{lst} may be modified to construct the returned list.")
  309. #define FUNC_NAME s_scm_srfi1_delete_x
  310. {
  311. SCM walk;
  312. SCM *prev;
  313. if (SCM_UNBNDP (pred))
  314. return scm_delete_x (x, lst);
  315. SCM_ASSERT (scm_is_true (scm_procedure_p (pred)), pred, SCM_ARG3, FUNC_NAME);
  316. for (prev = &lst, walk = lst;
  317. scm_is_pair (walk);
  318. walk = SCM_CDR (walk))
  319. {
  320. if (scm_is_true (scm_call_2 (pred, x, SCM_CAR (walk))))
  321. *prev = SCM_CDR (walk);
  322. else
  323. prev = SCM_CDRLOC (walk);
  324. }
  325. /* demand the input was a proper list */
  326. SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (walk), walk, SCM_ARG2, FUNC_NAME,"list");
  327. return lst;
  328. }
  329. #undef FUNC_NAME
  330. SCM_DEFINE (scm_srfi1_delete_duplicates, "delete-duplicates", 1, 1, 0,
  331. (SCM lst, SCM pred),
  332. "Return a list containing the elements of @var{lst} but without\n"
  333. "duplicates.\n"
  334. "\n"
  335. "When elements are equal, only the first in @var{lst} is\n"
  336. "retained. Equal elements can be anywhere in @var{lst}, they\n"
  337. "don't have to be adjacent. The returned list will have the\n"
  338. "retained elements in the same order as they were in @var{lst}.\n"
  339. "\n"
  340. "Equality is determined by @var{pred}, or @code{equal?} if not\n"
  341. "given. Calls @code{(pred x y)} are made with element @var{x}\n"
  342. "being before @var{y} in @var{lst}. A call is made at most once\n"
  343. "for each combination, but the sequence of the calls across the\n"
  344. "elements is unspecified.\n"
  345. "\n"
  346. "@var{lst} is not modified, but the return might share a common\n"
  347. "tail with @var{lst}.\n"
  348. "\n"
  349. "In the worst case, this is an @math{O(N^2)} algorithm because\n"
  350. "it must check each element against all those preceding it. For\n"
  351. "long lists it is more efficient to sort and then compare only\n"
  352. "adjacent elements.")
  353. #define FUNC_NAME s_scm_srfi1_delete_duplicates
  354. {
  355. scm_t_trampoline_2 equal_p;
  356. SCM ret, *p, keeplst, item, l;
  357. int count, i;
  358. /* ret is the new list constructed. p is where to append, initially &ret
  359. then SCM_CDRLOC of the last pair. lst is advanced as each element is
  360. considered.
  361. Elements retained are not immediately appended to ret, instead keeplst
  362. is the last pair in lst which is to be kept but is not yet copied.
  363. Initially this is the first pair of lst, since the first element is
  364. always retained.
  365. *p is kept set to keeplst, so ret (inclusive) to lst (exclusive) is all
  366. the elements retained, making the equality search loop easy.
  367. If an item must be deleted, elements from keeplst (inclusive) to lst
  368. (exclusive) must be copied and appended to ret. When there's no more
  369. deletions, *p is left set to keeplst, so ret shares structure with the
  370. original lst. (ret will be the entire original lst if there are no
  371. deletions.) */
  372. /* skip to end if an empty list (or something invalid) */
  373. ret = SCM_EOL;
  374. if (SCM_UNBNDP (pred))
  375. equal_p = equal_trampoline;
  376. else
  377. {
  378. SCM_VALIDATE_PROC (SCM_ARG2, pred);
  379. equal_p = scm_call_2;
  380. }
  381. keeplst = lst;
  382. count = 0;
  383. p = &ret;
  384. for ( ; scm_is_pair (lst); lst = SCM_CDR (lst))
  385. {
  386. item = SCM_CAR (lst);
  387. /* look for item in "ret" list */
  388. for (l = ret; scm_is_pair (l); l = SCM_CDR (l))
  389. {
  390. if (scm_is_true (equal_p (pred, SCM_CAR (l), item)))
  391. {
  392. /* "item" is a duplicate, so copy keeplst onto ret */
  393. duplicate:
  394. p = list_copy_part (keeplst, count, p);
  395. keeplst = SCM_CDR (lst); /* elem after the one deleted */
  396. count = 0;
  397. goto next_elem;
  398. }
  399. }
  400. /* look for item in "keeplst" list
  401. be careful traversing, in case nasty code changed the cdrs */
  402. for (i = 0, l = keeplst;
  403. i < count && scm_is_pair (l);
  404. i++, l = SCM_CDR (l))
  405. if (scm_is_true (equal_p (pred, SCM_CAR (l), item)))
  406. goto duplicate;
  407. /* keep this element */
  408. count++;
  409. next_elem:
  410. ;
  411. }
  412. SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst), lst, SCM_ARG1, FUNC_NAME, "list");
  413. /* share tail of keeplst items */
  414. *p = keeplst;
  415. return ret;
  416. }
  417. #undef FUNC_NAME
  418. SCM_DEFINE (scm_srfi1_delete_duplicates_x, "delete-duplicates!", 1, 1, 0,
  419. (SCM lst, SCM pred),
  420. "Return a list containing the elements of @var{lst} but without\n"
  421. "duplicates.\n"
  422. "\n"
  423. "When elements are equal, only the first in @var{lst} is\n"
  424. "retained. Equal elements can be anywhere in @var{lst}, they\n"
  425. "don't have to be adjacent. The returned list will have the\n"
  426. "retained elements in the same order as they were in @var{lst}.\n"
  427. "\n"
  428. "Equality is determined by @var{pred}, or @code{equal?} if not\n"
  429. "given. Calls @code{(pred x y)} are made with element @var{x}\n"
  430. "being before @var{y} in @var{lst}. A call is made at most once\n"
  431. "for each combination, but the sequence of the calls across the\n"
  432. "elements is unspecified.\n"
  433. "\n"
  434. "@var{lst} may be modified to construct the returned list.\n"
  435. "\n"
  436. "In the worst case, this is an @math{O(N^2)} algorithm because\n"
  437. "it must check each element against all those preceding it. For\n"
  438. "long lists it is more efficient to sort and then compare only\n"
  439. "adjacent elements.")
  440. #define FUNC_NAME s_scm_srfi1_delete_duplicates_x
  441. {
  442. scm_t_trampoline_2 equal_p;
  443. SCM ret, endret, item, l;
  444. /* ret is the return list, constructed from the pairs in lst. endret is
  445. the last pair of ret, initially the first pair. lst is advanced as
  446. elements are considered. */
  447. /* skip to end if an empty list (or something invalid) */
  448. ret = lst;
  449. if (scm_is_pair (lst))
  450. {
  451. if (SCM_UNBNDP (pred))
  452. equal_p = equal_trampoline;
  453. else
  454. {
  455. SCM_VALIDATE_PROC (SCM_ARG2, pred);
  456. equal_p = scm_call_2;
  457. }
  458. endret = ret;
  459. /* loop over lst elements starting from second */
  460. for (;;)
  461. {
  462. lst = SCM_CDR (lst);
  463. if (! scm_is_pair (lst))
  464. break;
  465. item = SCM_CAR (lst);
  466. /* is item equal to any element from ret to endret (inclusive)? */
  467. l = ret;
  468. for (;;)
  469. {
  470. if (scm_is_true (equal_p (pred, SCM_CAR (l), item)))
  471. break; /* equal, forget this element */
  472. if (scm_is_eq (l, endret))
  473. {
  474. /* not equal to any, so append this pair */
  475. SCM_SETCDR (endret, lst);
  476. endret = lst;
  477. break;
  478. }
  479. l = SCM_CDR (l);
  480. }
  481. }
  482. /* terminate, in case last element was deleted */
  483. SCM_SETCDR (endret, SCM_EOL);
  484. }
  485. /* demand that lst was a proper list */
  486. SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst), lst, SCM_ARG1, FUNC_NAME, "list");
  487. return ret;
  488. }
  489. #undef FUNC_NAME
  490. SCM_DEFINE (scm_srfi1_find, "find", 2, 0, 0,
  491. (SCM pred, SCM lst),
  492. "Return the first element of @var{lst} which satisfies the\n"
  493. "predicate @var{pred}, or return @code{#f} if no such element is\n"
  494. "found.")
  495. #define FUNC_NAME s_scm_srfi1_find
  496. {
  497. SCM_VALIDATE_PROC (SCM_ARG1, pred);
  498. for ( ; scm_is_pair (lst); lst = SCM_CDR (lst))
  499. {
  500. SCM elem = SCM_CAR (lst);
  501. if (scm_is_true (scm_call_1 (pred, elem)))
  502. return elem;
  503. }
  504. SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst), lst, SCM_ARG2, FUNC_NAME, "list");
  505. return SCM_BOOL_F;
  506. }
  507. #undef FUNC_NAME
  508. SCM_DEFINE (scm_srfi1_find_tail, "find-tail", 2, 0, 0,
  509. (SCM pred, SCM lst),
  510. "Return the first pair of @var{lst} whose @sc{car} satisfies the\n"
  511. "predicate @var{pred}, or return @code{#f} if no such element is\n"
  512. "found.")
  513. #define FUNC_NAME s_scm_srfi1_find_tail
  514. {
  515. SCM_VALIDATE_PROC (SCM_ARG1, pred);
  516. for ( ; scm_is_pair (lst); lst = SCM_CDR (lst))
  517. if (scm_is_true (scm_call_1 (pred, SCM_CAR (lst))))
  518. return lst;
  519. SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst), lst, SCM_ARG2, FUNC_NAME, "list");
  520. return SCM_BOOL_F;
  521. }
  522. #undef FUNC_NAME
  523. SCM_DEFINE (scm_srfi1_length_plus, "length+", 1, 0, 0,
  524. (SCM lst),
  525. "Return the length of @var{lst}, or @code{#f} if @var{lst} is\n"
  526. "circular.")
  527. #define FUNC_NAME s_scm_srfi1_length_plus
  528. {
  529. size_t i = 0;
  530. SCM tortoise = lst;
  531. SCM hare = lst;
  532. do
  533. {
  534. if (!scm_is_pair (hare))
  535. {
  536. if (SCM_NULL_OR_NIL_P (hare))
  537. return scm_from_size_t (i);
  538. else
  539. scm_wrong_type_arg_msg (FUNC_NAME, 1, lst,
  540. "proper or circular list");
  541. }
  542. hare = SCM_CDR (hare);
  543. i++;
  544. if (!scm_is_pair (hare))
  545. {
  546. if (SCM_NULL_OR_NIL_P (hare))
  547. return scm_from_size_t (i);
  548. else
  549. scm_wrong_type_arg_msg (FUNC_NAME, 1, lst,
  550. "proper or circular list");
  551. }
  552. hare = SCM_CDR (hare);
  553. i++;
  554. /* For every two steps the hare takes, the tortoise takes one. */
  555. tortoise = SCM_CDR (tortoise);
  556. }
  557. while (!scm_is_eq (hare, tortoise));
  558. /* If the tortoise ever catches the hare, then the list must contain
  559. a cycle. */
  560. return SCM_BOOL_F;
  561. }
  562. #undef FUNC_NAME
  563. /* This routine differs from the core list-copy in allowing improper lists.
  564. Maybe the core could allow them similarly. */
  565. SCM_DEFINE (scm_srfi1_list_copy, "list-copy", 1, 0, 0,
  566. (SCM lst),
  567. "Return a copy of the given list @var{lst}.\n"
  568. "\n"
  569. "@var{lst} can be a proper or improper list. And if @var{lst}\n"
  570. "is not a pair then it's treated as the final tail of an\n"
  571. "improper list and simply returned.")
  572. #define FUNC_NAME s_scm_srfi1_list_copy
  573. {
  574. SCM newlst;
  575. SCM * fill_here;
  576. SCM from_here;
  577. newlst = lst;
  578. fill_here = &newlst;
  579. from_here = lst;
  580. while (scm_is_pair (from_here))
  581. {
  582. SCM c;
  583. c = scm_cons (SCM_CAR (from_here), SCM_CDR (from_here));
  584. *fill_here = c;
  585. fill_here = SCM_CDRLOC (c);
  586. from_here = SCM_CDR (from_here);
  587. }
  588. return newlst;
  589. }
  590. #undef FUNC_NAME
  591. SCM_DEFINE (scm_srfi1_lset_difference_x, "lset-difference!", 2, 0, 1,
  592. (SCM equal, SCM lst, SCM rest),
  593. "Return @var{lst} with any elements in the lists in @var{rest}\n"
  594. "removed (ie.@: subtracted). For only one @var{lst} argument,\n"
  595. "just that list is returned.\n"
  596. "\n"
  597. "The given @var{equal} procedure is used for comparing elements,\n"
  598. "called as @code{(@var{equal} elem1 elemN)}. The first argument\n"
  599. "is from @var{lst} and the second from one of the subsequent\n"
  600. "lists. But exactly which calls are made and in what order is\n"
  601. "unspecified.\n"
  602. "\n"
  603. "@example\n"
  604. "(lset-difference! eqv? (list 'x 'y)) @result{} (x y)\n"
  605. "(lset-difference! eqv? (list 1 2 3) '(3 1)) @result{} (2)\n"
  606. "(lset-difference! eqv? (list 1 2 3) '(3) '(2)) @result{} (1)\n"
  607. "@end example\n"
  608. "\n"
  609. "@code{lset-difference!} may modify @var{lst} to form its\n"
  610. "result.")
  611. #define FUNC_NAME s_scm_srfi1_lset_difference_x
  612. {
  613. SCM ret, *pos, elem, r, b;
  614. int argnum;
  615. SCM_VALIDATE_PROC (SCM_ARG1, equal);
  616. SCM_VALIDATE_REST_ARGUMENT (rest);
  617. ret = SCM_EOL;
  618. pos = &ret;
  619. for ( ; scm_is_pair (lst); lst = SCM_CDR (lst))
  620. {
  621. elem = SCM_CAR (lst);
  622. for (r = rest, argnum = SCM_ARG3;
  623. scm_is_pair (r);
  624. r = SCM_CDR (r), argnum++)
  625. {
  626. for (b = SCM_CAR (r); scm_is_pair (b); b = SCM_CDR (b))
  627. if (scm_is_true (scm_call_2 (equal, elem, SCM_CAR (b))))
  628. goto next_elem; /* equal to elem, so drop that elem */
  629. SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (b), b, argnum, FUNC_NAME,"list");
  630. }
  631. /* elem not equal to anything in later lists, so keep it */
  632. *pos = lst;
  633. pos = SCM_CDRLOC (lst);
  634. next_elem:
  635. ;
  636. }
  637. SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst), lst, SCM_ARG2, FUNC_NAME, "list");
  638. *pos = SCM_EOL;
  639. return ret;
  640. }
  641. #undef FUNC_NAME
  642. SCM_DEFINE (scm_srfi1_assoc, "assoc", 2, 1, 0,
  643. (SCM key, SCM alist, SCM pred),
  644. "Behaves like @code{assq} but uses third argument @var{pred}\n"
  645. "for key comparison. If @var{pred} is not supplied,\n"
  646. "@code{equal?} is used. (Extended from R5RS.)\n")
  647. #define FUNC_NAME s_scm_srfi1_assoc
  648. {
  649. SCM ls = alist;
  650. scm_t_trampoline_2 equal_p;
  651. if (SCM_UNBNDP (pred))
  652. equal_p = equal_trampoline;
  653. else
  654. {
  655. SCM_VALIDATE_PROC (SCM_ARG3, pred);
  656. equal_p = scm_call_2;
  657. }
  658. for(; scm_is_pair (ls); ls = SCM_CDR (ls))
  659. {
  660. SCM tmp = SCM_CAR (ls);
  661. SCM_ASSERT_TYPE (scm_is_pair (tmp), alist, SCM_ARG2, FUNC_NAME,
  662. "association list");
  663. if (scm_is_true (equal_p (pred, key, SCM_CAR (tmp))))
  664. return tmp;
  665. }
  666. SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (ls), alist, SCM_ARG2, FUNC_NAME,
  667. "association list");
  668. return SCM_BOOL_F;
  669. }
  670. #undef FUNC_NAME
  671. SCM_DEFINE (scm_srfi1_partition, "partition", 2, 0, 0,
  672. (SCM pred, SCM list),
  673. "Partition the elements of @var{list} with predicate @var{pred}.\n"
  674. "Return two values: the list of elements satisfying @var{pred} and\n"
  675. "the list of elements @emph{not} satisfying @var{pred}. The order\n"
  676. "of the output lists follows the order of @var{list}. @var{list}\n"
  677. "is not mutated. One of the output lists may share memory with @var{list}.\n")
  678. #define FUNC_NAME s_scm_srfi1_partition
  679. {
  680. /* In this implementation, the output lists don't share memory with
  681. list, because it's probably not worth the effort. */
  682. SCM orig_list = list;
  683. SCM kept = scm_cons(SCM_EOL, SCM_EOL);
  684. SCM kept_tail = kept;
  685. SCM dropped = scm_cons(SCM_EOL, SCM_EOL);
  686. SCM dropped_tail = dropped;
  687. SCM_VALIDATE_PROC (SCM_ARG1, pred);
  688. for (; !SCM_NULL_OR_NIL_P (list); list = SCM_CDR(list)) {
  689. SCM elt, new_tail;
  690. /* Make sure LIST is not a dotted list. */
  691. SCM_ASSERT (scm_is_pair (list), orig_list, SCM_ARG2, FUNC_NAME);
  692. elt = SCM_CAR (list);
  693. new_tail = scm_cons (SCM_CAR (list), SCM_EOL);
  694. if (scm_is_true (scm_call_1 (pred, elt))) {
  695. SCM_SETCDR(kept_tail, new_tail);
  696. kept_tail = new_tail;
  697. }
  698. else {
  699. SCM_SETCDR(dropped_tail, new_tail);
  700. dropped_tail = new_tail;
  701. }
  702. }
  703. /* re-use the initial conses for the values list */
  704. SCM_SETCAR(kept, SCM_CDR(kept));
  705. SCM_SETCDR(kept, dropped);
  706. SCM_SETCAR(dropped, SCM_CDR(dropped));
  707. SCM_SETCDR(dropped, SCM_EOL);
  708. return scm_values(kept);
  709. }
  710. #undef FUNC_NAME
  711. SCM_DEFINE (scm_srfi1_partition_x, "partition!", 2, 0, 0,
  712. (SCM pred, SCM lst),
  713. "Split @var{lst} into those elements which do and don't satisfy\n"
  714. "the predicate @var{pred}.\n"
  715. "\n"
  716. "The return is two values (@pxref{Multiple Values}), the first\n"
  717. "being a list of all elements from @var{lst} which satisfy\n"
  718. "@var{pred}, the second a list of those which do not.\n"
  719. "\n"
  720. "The elements in the result lists are in the same order as in\n"
  721. "@var{lst} but the order in which the calls @code{(@var{pred}\n"
  722. "elem)} are made on the list elements is unspecified.\n"
  723. "\n"
  724. "@var{lst} may be modified to construct the return lists.")
  725. #define FUNC_NAME s_scm_srfi1_partition_x
  726. {
  727. SCM tlst, flst, *tp, *fp;
  728. SCM_ASSERT (scm_is_true (scm_procedure_p (pred)), pred, SCM_ARG1, FUNC_NAME);
  729. /* tlst and flst are the lists of true and false elements. tp and fp are
  730. where to store to append to them, initially &tlst and &flst, then
  731. SCM_CDRLOC of the last pair in the respective lists. */
  732. tlst = SCM_EOL;
  733. flst = SCM_EOL;
  734. tp = &tlst;
  735. fp = &flst;
  736. for ( ; scm_is_pair (lst); lst = SCM_CDR (lst))
  737. {
  738. if (scm_is_true (scm_call_1 (pred, SCM_CAR (lst))))
  739. {
  740. *tp = lst;
  741. tp = SCM_CDRLOC (lst);
  742. }
  743. else
  744. {
  745. *fp = lst;
  746. fp = SCM_CDRLOC (lst);
  747. }
  748. }
  749. SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst), lst, SCM_ARG2, FUNC_NAME, "list");
  750. /* terminate whichever didn't get the last element(s) */
  751. *tp = SCM_EOL;
  752. *fp = SCM_EOL;
  753. return scm_values (scm_list_2 (tlst, flst));
  754. }
  755. #undef FUNC_NAME
  756. SCM_DEFINE (scm_srfi1_remove, "remove", 2, 0, 0,
  757. (SCM pred, SCM list),
  758. "Return a list containing all elements from @var{list} which do\n"
  759. "not satisfy the predicate @var{pred}. The elements in the\n"
  760. "result list have the same order as in @var{list}. The order in\n"
  761. "which @var{pred} is applied to the list elements is not\n"
  762. "specified.")
  763. #define FUNC_NAME s_scm_srfi1_remove
  764. {
  765. SCM walk;
  766. SCM *prev;
  767. SCM res = SCM_EOL;
  768. SCM_VALIDATE_PROC (SCM_ARG1, pred);
  769. SCM_VALIDATE_LIST (2, list);
  770. for (prev = &res, walk = list;
  771. scm_is_pair (walk);
  772. walk = SCM_CDR (walk))
  773. {
  774. if (scm_is_false (scm_call_1 (pred, SCM_CAR (walk))))
  775. {
  776. *prev = scm_cons (SCM_CAR (walk), SCM_EOL);
  777. prev = SCM_CDRLOC (*prev);
  778. }
  779. }
  780. return res;
  781. }
  782. #undef FUNC_NAME
  783. SCM_DEFINE (scm_srfi1_remove_x, "remove!", 2, 0, 0,
  784. (SCM pred, SCM list),
  785. "Return a list containing all elements from @var{list} which do\n"
  786. "not satisfy the predicate @var{pred}. The elements in the\n"
  787. "result list have the same order as in @var{list}. The order in\n"
  788. "which @var{pred} is applied to the list elements is not\n"
  789. "specified. @var{list} may be modified to build the return\n"
  790. "list.")
  791. #define FUNC_NAME s_scm_srfi1_remove_x
  792. {
  793. SCM walk;
  794. SCM *prev;
  795. SCM_VALIDATE_PROC (SCM_ARG1, pred);
  796. SCM_VALIDATE_LIST (2, list);
  797. for (prev = &list, walk = list;
  798. scm_is_pair (walk);
  799. walk = SCM_CDR (walk))
  800. {
  801. if (scm_is_false (scm_call_1 (pred, SCM_CAR (walk))))
  802. prev = SCM_CDRLOC (walk);
  803. else
  804. *prev = SCM_CDR (walk);
  805. }
  806. return list;
  807. }
  808. #undef FUNC_NAME
  809. void
  810. scm_register_srfi_1 (void)
  811. {
  812. scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION,
  813. "scm_init_srfi_1",
  814. (scm_t_extension_init_func)scm_init_srfi_1, NULL);
  815. }
  816. void
  817. scm_init_srfi_1 (void)
  818. {
  819. #ifndef SCM_MAGIC_SNARFER
  820. #include "libguile/srfi-1.x"
  821. #endif
  822. }
  823. /* End of srfi-1.c. */