srfi-1.c 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969
  1. /* srfi-1.c --- SRFI-1 procedures for Guile
  2. *
  3. * Copyright (C) 1995, 1996, 1997, 2000, 2001, 2002, 2003, 2005, 2006, 2008, 2009, 2010
  4. * 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.h>
  25. #include "srfi-1.h"
  26. /* The intent of this file was to gradually replace those Scheme
  27. * procedures in srfi-1.scm that extend core primitive procedures,
  28. * so that using srfi-1 won't have performance penalties.
  29. *
  30. * However, we now prefer to write these procedures in Scheme, let the compiler
  31. * optimize them, and have the VM execute them efficiently.
  32. */
  33. /* The `(srfi srfi-1)' module. */
  34. static SCM srfi1_module = SCM_BOOL_F;
  35. /* Cache variable NAME in C variable VAR. */
  36. #define CACHE_VAR(var, name) \
  37. static SCM var = SCM_BOOL_F; \
  38. if (scm_is_false (var)) \
  39. { \
  40. if (SCM_UNLIKELY (scm_is_false (srfi1_module))) \
  41. srfi1_module = scm_c_resolve_module ("srfi srfi-1"); \
  42. \
  43. var = scm_module_variable (srfi1_module, \
  44. scm_from_locale_symbol (name)); \
  45. if (SCM_UNLIKELY (scm_is_false (var))) \
  46. abort (); \
  47. \
  48. var = SCM_VARIABLE_REF (var); \
  49. }
  50. static long
  51. srfi1_ilength (SCM sx)
  52. {
  53. long i = 0;
  54. SCM tortoise = sx;
  55. SCM hare = sx;
  56. do {
  57. if (SCM_NULL_OR_NIL_P(hare)) return i;
  58. if (!scm_is_pair (hare)) return -2;
  59. hare = SCM_CDR(hare);
  60. i++;
  61. if (SCM_NULL_OR_NIL_P(hare)) return i;
  62. if (!scm_is_pair (hare)) return -2;
  63. hare = SCM_CDR(hare);
  64. i++;
  65. /* For every two steps the hare takes, the tortoise takes one. */
  66. tortoise = SCM_CDR(tortoise);
  67. }
  68. while (! scm_is_eq (hare, tortoise));
  69. /* If the tortoise ever catches the hare, then the list must contain
  70. a cycle. */
  71. return -1;
  72. }
  73. static SCM
  74. equal_trampoline (SCM proc, SCM arg1, SCM arg2)
  75. {
  76. return scm_equal_p (arg1, arg2);
  77. }
  78. /* list_copy_part() copies the first COUNT cells of LST, puts the result at
  79. *dst, and returns the SCM_CDRLOC of the last cell in that new list.
  80. This function is designed to be careful about LST possibly having changed
  81. in between the caller deciding what to copy, and the copy actually being
  82. done here. The COUNT ensures we terminate if LST has become circular,
  83. SCM_VALIDATE_CONS guards against a cdr in the list changed to some
  84. non-pair object. */
  85. #include <stdio.h>
  86. static SCM *
  87. list_copy_part (SCM lst, int count, SCM *dst)
  88. #define FUNC_NAME "list_copy_part"
  89. {
  90. SCM c;
  91. for ( ; count > 0; count--)
  92. {
  93. SCM_VALIDATE_CONS (SCM_ARGn, lst);
  94. c = scm_cons (SCM_CAR (lst), SCM_EOL);
  95. *dst = c;
  96. dst = SCM_CDRLOC (c);
  97. lst = SCM_CDR (lst);
  98. }
  99. return dst;
  100. }
  101. #undef FUNC_NAME
  102. SCM_DEFINE (scm_srfi1_alist_copy, "alist-copy", 1, 0, 0,
  103. (SCM alist),
  104. "Return a copy of @var{alist}, copying both the pairs comprising\n"
  105. "the list and those making the associations.")
  106. #define FUNC_NAME s_scm_srfi1_alist_copy
  107. {
  108. SCM ret, *p, elem, c;
  109. /* ret is the list to return. p is where to append to it, initially &ret
  110. then SCM_CDRLOC of the last pair. */
  111. ret = SCM_EOL;
  112. p = &ret;
  113. for ( ; scm_is_pair (alist); alist = SCM_CDR (alist))
  114. {
  115. elem = SCM_CAR (alist);
  116. /* each element of alist must be a pair */
  117. SCM_ASSERT_TYPE (scm_is_pair (elem), alist, SCM_ARG1, FUNC_NAME,
  118. "association list");
  119. c = scm_cons (scm_cons (SCM_CAR (elem), SCM_CDR (elem)), SCM_EOL);
  120. *p = c;
  121. p = SCM_CDRLOC (c);
  122. }
  123. /* alist must be a proper list */
  124. SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (alist), alist, SCM_ARG1, FUNC_NAME,
  125. "association list");
  126. return ret;
  127. }
  128. #undef FUNC_NAME
  129. SCM_DEFINE (scm_srfi1_append_reverse, "append-reverse", 2, 0, 0,
  130. (SCM revhead, SCM tail),
  131. "Reverse @var{rev-head}, append @var{tail} to it, and return the\n"
  132. "result. This is equivalent to @code{(append (reverse\n"
  133. "@var{rev-head}) @var{tail})}, but its implementation is more\n"
  134. "efficient.\n"
  135. "\n"
  136. "@example\n"
  137. "(append-reverse '(1 2 3) '(4 5 6)) @result{} (3 2 1 4 5 6)\n"
  138. "@end example")
  139. #define FUNC_NAME s_scm_srfi1_append_reverse
  140. {
  141. while (scm_is_pair (revhead))
  142. {
  143. /* copy first element of revhead onto front of tail */
  144. tail = scm_cons (SCM_CAR (revhead), tail);
  145. revhead = SCM_CDR (revhead);
  146. }
  147. SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (revhead), revhead, SCM_ARG1, FUNC_NAME,
  148. "list");
  149. return tail;
  150. }
  151. #undef FUNC_NAME
  152. SCM_DEFINE (scm_srfi1_append_reverse_x, "append-reverse!", 2, 0, 0,
  153. (SCM revhead, SCM tail),
  154. "Reverse @var{rev-head}, append @var{tail} to it, and return the\n"
  155. "result. This is equivalent to @code{(append! (reverse!\n"
  156. "@var{rev-head}) @var{tail})}, but its implementation is more\n"
  157. "efficient.\n"
  158. "\n"
  159. "@example\n"
  160. "(append-reverse! (list 1 2 3) '(4 5 6)) @result{} (3 2 1 4 5 6)\n"
  161. "@end example\n"
  162. "\n"
  163. "@var{rev-head} may be modified in order to produce the result.")
  164. #define FUNC_NAME s_scm_srfi1_append_reverse_x
  165. {
  166. SCM newtail;
  167. while (scm_is_pair (revhead))
  168. {
  169. /* take the first cons cell from revhead */
  170. newtail = revhead;
  171. revhead = SCM_CDR (revhead);
  172. /* make it the new start of tail, appending the previous */
  173. SCM_SETCDR (newtail, tail);
  174. tail = newtail;
  175. }
  176. SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (revhead), revhead, SCM_ARG1, FUNC_NAME,
  177. "list");
  178. return tail;
  179. }
  180. #undef FUNC_NAME
  181. SCM_DEFINE (scm_srfi1_break, "break", 2, 0, 0,
  182. (SCM pred, SCM lst),
  183. "Return two values, the longest initial prefix of @var{lst}\n"
  184. "whose elements all fail the predicate @var{pred}, and the\n"
  185. "remainder of @var{lst}.\n"
  186. "\n"
  187. "Note that the name @code{break} conflicts with the @code{break}\n"
  188. "binding established by @code{while}. Applications wanting to\n"
  189. "use @code{break} from within a @code{while} loop will need to\n"
  190. "make a new define under a different name.")
  191. #define FUNC_NAME s_scm_srfi1_break
  192. {
  193. SCM ret, *p;
  194. SCM_ASSERT (scm_is_true (scm_procedure_p (pred)), pred, SCM_ARG1, FUNC_NAME);
  195. ret = SCM_EOL;
  196. p = &ret;
  197. for ( ; scm_is_pair (lst); lst = SCM_CDR (lst))
  198. {
  199. SCM elem = SCM_CAR (lst);
  200. if (scm_is_true (scm_call_1 (pred, elem)))
  201. goto done;
  202. /* want this elem, tack it onto the end of ret */
  203. *p = scm_cons (elem, SCM_EOL);
  204. p = SCM_CDRLOC (*p);
  205. }
  206. SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst), lst, SCM_ARG2, FUNC_NAME, "list");
  207. done:
  208. return scm_values (scm_list_2 (ret, lst));
  209. }
  210. #undef FUNC_NAME
  211. SCM_DEFINE (scm_srfi1_break_x, "break!", 2, 0, 0,
  212. (SCM pred, SCM lst),
  213. "Return two values, the longest initial prefix of @var{lst}\n"
  214. "whose elements all fail the predicate @var{pred}, and the\n"
  215. "remainder of @var{lst}. @var{lst} may be modified to form the\n"
  216. "return.")
  217. #define FUNC_NAME s_scm_srfi1_break_x
  218. {
  219. SCM upto, *p;
  220. SCM_ASSERT (scm_is_true (scm_procedure_p (pred)), pred, SCM_ARG1, FUNC_NAME);
  221. p = &lst;
  222. for (upto = lst; scm_is_pair (upto); upto = SCM_CDR (upto))
  223. {
  224. if (scm_is_true (scm_call_1 (pred, SCM_CAR (upto))))
  225. goto done;
  226. /* want this element */
  227. p = SCM_CDRLOC (upto);
  228. }
  229. SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (upto), lst, SCM_ARG2, FUNC_NAME, "list");
  230. done:
  231. *p = SCM_EOL;
  232. return scm_values (scm_list_2 (lst, upto));
  233. }
  234. #undef FUNC_NAME
  235. SCM
  236. scm_srfi1_car_plus_cdr (SCM pair)
  237. {
  238. CACHE_VAR (car_plus_cdr, "car+cdr");
  239. return scm_call_1 (car_plus_cdr, pair);
  240. }
  241. SCM_DEFINE (scm_srfi1_concatenate, "concatenate", 1, 0, 0,
  242. (SCM lstlst),
  243. "Construct a list by appending all lists in @var{lstlst}.\n"
  244. "\n"
  245. "@code{concatenate} is the same as @code{(apply append\n"
  246. "@var{lstlst})}. It exists because some Scheme implementations\n"
  247. "have a limit on the number of arguments a function takes, which\n"
  248. "the @code{apply} might exceed. In Guile there is no such\n"
  249. "limit.")
  250. #define FUNC_NAME s_scm_srfi1_concatenate
  251. {
  252. SCM_VALIDATE_LIST (SCM_ARG1, lstlst);
  253. return scm_append (lstlst);
  254. }
  255. #undef FUNC_NAME
  256. SCM_DEFINE (scm_srfi1_concatenate_x, "concatenate!", 1, 0, 0,
  257. (SCM lstlst),
  258. "Construct a list by appending all lists in @var{lstlst}. Those\n"
  259. "lists may be modified to produce the result.\n"
  260. "\n"
  261. "@code{concatenate!} is the same as @code{(apply append!\n"
  262. "@var{lstlst})}. It exists because some Scheme implementations\n"
  263. "have a limit on the number of arguments a function takes, which\n"
  264. "the @code{apply} might exceed. In Guile there is no such\n"
  265. "limit.")
  266. #define FUNC_NAME s_scm_srfi1_concatenate
  267. {
  268. SCM_VALIDATE_LIST (SCM_ARG1, lstlst);
  269. return scm_append_x (lstlst);
  270. }
  271. #undef FUNC_NAME
  272. SCM_DEFINE (scm_srfi1_count, "count", 2, 0, 1,
  273. (SCM pred, SCM list1, SCM rest),
  274. "Return a count of the number of times @var{pred} returns true\n"
  275. "when called on elements from the given lists.\n"
  276. "\n"
  277. "@var{pred} is called with @var{N} parameters @code{(@var{pred}\n"
  278. "@var{elem1} @dots{} @var{elemN})}, each element being from the\n"
  279. "corresponding @var{list1} @dots{} @var{lstN}. The first call is\n"
  280. "with the first element of each list, the second with the second\n"
  281. "element from each, and so on.\n"
  282. "\n"
  283. "Counting stops when the end of the shortest list is reached.\n"
  284. "At least one list must be non-circular.")
  285. #define FUNC_NAME s_scm_srfi1_count
  286. {
  287. long count;
  288. SCM lst;
  289. int argnum;
  290. SCM_VALIDATE_REST_ARGUMENT (rest);
  291. count = 0;
  292. if (scm_is_null (rest))
  293. {
  294. /* one list */
  295. SCM_ASSERT (scm_is_true (scm_procedure_p (pred)), pred, SCM_ARG1, FUNC_NAME);
  296. for ( ; scm_is_pair (list1); list1 = SCM_CDR (list1))
  297. count += scm_is_true (scm_call_1 (pred, SCM_CAR (list1)));
  298. /* check below that list1 is a proper list, and done */
  299. end_list1:
  300. lst = list1;
  301. argnum = 2;
  302. }
  303. else if (scm_is_pair (rest) && scm_is_null (SCM_CDR (rest)))
  304. {
  305. /* two lists */
  306. SCM list2;
  307. SCM_ASSERT (scm_is_true (scm_procedure_p (pred)), pred, SCM_ARG1, FUNC_NAME);
  308. list2 = SCM_CAR (rest);
  309. for (;;)
  310. {
  311. if (! scm_is_pair (list1))
  312. goto end_list1;
  313. if (! scm_is_pair (list2))
  314. {
  315. lst = list2;
  316. argnum = 3;
  317. break;
  318. }
  319. count += scm_is_true (scm_call_2
  320. (pred, SCM_CAR (list1), SCM_CAR (list2)));
  321. list1 = SCM_CDR (list1);
  322. list2 = SCM_CDR (list2);
  323. }
  324. }
  325. else
  326. {
  327. /* three or more lists */
  328. SCM vec, args, a;
  329. size_t len, i;
  330. /* vec is the list arguments */
  331. vec = scm_vector (scm_cons (list1, rest));
  332. len = SCM_SIMPLE_VECTOR_LENGTH (vec);
  333. /* args is the argument list to pass to pred, same length as vec,
  334. re-used for each call */
  335. args = scm_make_list (SCM_I_MAKINUM (len), SCM_UNDEFINED);
  336. for (;;)
  337. {
  338. /* first elem of each list in vec into args, and step those
  339. vec entries onto their next element */
  340. for (i = 0, a = args, argnum = 2;
  341. i < len;
  342. i++, a = SCM_CDR (a), argnum++)
  343. {
  344. lst = SCM_SIMPLE_VECTOR_REF (vec, i); /* list argument */
  345. if (! scm_is_pair (lst))
  346. goto check_lst_and_done;
  347. SCM_SETCAR (a, SCM_CAR (lst)); /* arg for pred */
  348. SCM_SIMPLE_VECTOR_SET (vec, i, SCM_CDR (lst)); /* rest of lst */
  349. }
  350. count += scm_is_true (scm_apply (pred, args, SCM_EOL));
  351. }
  352. }
  353. check_lst_and_done:
  354. SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst), lst, argnum, FUNC_NAME, "list");
  355. return scm_from_long (count);
  356. }
  357. #undef FUNC_NAME
  358. SCM_DEFINE (scm_srfi1_delete, "delete", 2, 1, 0,
  359. (SCM x, SCM lst, SCM pred),
  360. "Return a list containing the elements of @var{lst} but with\n"
  361. "those equal to @var{x} deleted. The returned elements will be\n"
  362. "in the same order as they were in @var{lst}.\n"
  363. "\n"
  364. "Equality is determined by @var{pred}, or @code{equal?} if not\n"
  365. "given. An equality call is made just once for each element,\n"
  366. "but the order in which the calls are made on the elements is\n"
  367. "unspecified.\n"
  368. "\n"
  369. "The equality calls are always @code{(pred x elem)}, ie.@: the\n"
  370. "given @var{x} is first. This means for instance elements\n"
  371. "greater than 5 can be deleted with @code{(delete 5 lst <)}.\n"
  372. "\n"
  373. "@var{lst} is not modified, but the returned list might share a\n"
  374. "common tail with @var{lst}.")
  375. #define FUNC_NAME s_scm_srfi1_delete
  376. {
  377. SCM ret, *p, keeplst;
  378. int count;
  379. if (SCM_UNBNDP (pred))
  380. return scm_delete (x, lst);
  381. SCM_ASSERT (scm_is_true (scm_procedure_p (pred)), pred, SCM_ARG3, FUNC_NAME);
  382. /* ret is the return list being constructed. p is where to append to it,
  383. initially &ret then SCM_CDRLOC of the last pair. lst progresses as
  384. elements are considered.
  385. Elements to be retained are not immediately copied, instead keeplst is
  386. the last pair in lst which is to be retained but not yet copied, count
  387. is how many from there are wanted. When there's no more deletions, *p
  388. can be set to keeplst to share the remainder of the original lst. (The
  389. entire original lst if there's no deletions at all.) */
  390. keeplst = lst;
  391. count = 0;
  392. p = &ret;
  393. for ( ; scm_is_pair (lst); lst = SCM_CDR (lst))
  394. {
  395. if (scm_is_true (scm_call_2 (pred, x, SCM_CAR (lst))))
  396. {
  397. /* delete this element, so copy those at keeplst */
  398. p = list_copy_part (keeplst, count, p);
  399. keeplst = SCM_CDR (lst);
  400. count = 0;
  401. }
  402. else
  403. {
  404. /* keep this element */
  405. count++;
  406. }
  407. }
  408. /* final retained elements */
  409. *p = keeplst;
  410. /* demand that lst was a proper list */
  411. SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst), lst, SCM_ARG2, FUNC_NAME, "list");
  412. return ret;
  413. }
  414. #undef FUNC_NAME
  415. SCM_DEFINE (scm_srfi1_delete_x, "delete!", 2, 1, 0,
  416. (SCM x, SCM lst, SCM pred),
  417. "Return a list containing the elements of @var{lst} but with\n"
  418. "those equal to @var{x} deleted. The returned elements will be\n"
  419. "in the same order as they were in @var{lst}.\n"
  420. "\n"
  421. "Equality is determined by @var{pred}, or @code{equal?} if not\n"
  422. "given. An equality call is made just once for each element,\n"
  423. "but the order in which the calls are made on the elements is\n"
  424. "unspecified.\n"
  425. "\n"
  426. "The equality calls are always @code{(pred x elem)}, ie.@: the\n"
  427. "given @var{x} is first. This means for instance elements\n"
  428. "greater than 5 can be deleted with @code{(delete 5 lst <)}.\n"
  429. "\n"
  430. "@var{lst} may be modified to construct the returned list.")
  431. #define FUNC_NAME s_scm_srfi1_delete_x
  432. {
  433. SCM walk;
  434. SCM *prev;
  435. if (SCM_UNBNDP (pred))
  436. return scm_delete_x (x, lst);
  437. SCM_ASSERT (scm_is_true (scm_procedure_p (pred)), pred, SCM_ARG3, FUNC_NAME);
  438. for (prev = &lst, walk = lst;
  439. scm_is_pair (walk);
  440. walk = SCM_CDR (walk))
  441. {
  442. if (scm_is_true (scm_call_2 (pred, x, SCM_CAR (walk))))
  443. *prev = SCM_CDR (walk);
  444. else
  445. prev = SCM_CDRLOC (walk);
  446. }
  447. /* demand the input was a proper list */
  448. SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (walk), walk, SCM_ARG2, FUNC_NAME,"list");
  449. return lst;
  450. }
  451. #undef FUNC_NAME
  452. SCM_DEFINE (scm_srfi1_delete_duplicates, "delete-duplicates", 1, 1, 0,
  453. (SCM lst, SCM pred),
  454. "Return a list containing the elements of @var{lst} but without\n"
  455. "duplicates.\n"
  456. "\n"
  457. "When elements are equal, only the first in @var{lst} is\n"
  458. "retained. Equal elements can be anywhere in @var{lst}, they\n"
  459. "don't have to be adjacent. The returned list will have the\n"
  460. "retained elements in the same order as they were in @var{lst}.\n"
  461. "\n"
  462. "Equality is determined by @var{pred}, or @code{equal?} if not\n"
  463. "given. Calls @code{(pred x y)} are made with element @var{x}\n"
  464. "being before @var{y} in @var{lst}. A call is made at most once\n"
  465. "for each combination, but the sequence of the calls across the\n"
  466. "elements is unspecified.\n"
  467. "\n"
  468. "@var{lst} is not modified, but the return might share a common\n"
  469. "tail with @var{lst}.\n"
  470. "\n"
  471. "In the worst case, this is an @math{O(N^2)} algorithm because\n"
  472. "it must check each element against all those preceding it. For\n"
  473. "long lists it is more efficient to sort and then compare only\n"
  474. "adjacent elements.")
  475. #define FUNC_NAME s_scm_srfi1_delete_duplicates
  476. {
  477. scm_t_trampoline_2 equal_p;
  478. SCM ret, *p, keeplst, item, l;
  479. int count, i;
  480. /* ret is the new list constructed. p is where to append, initially &ret
  481. then SCM_CDRLOC of the last pair. lst is advanced as each element is
  482. considered.
  483. Elements retained are not immediately appended to ret, instead keeplst
  484. is the last pair in lst which is to be kept but is not yet copied.
  485. Initially this is the first pair of lst, since the first element is
  486. always retained.
  487. *p is kept set to keeplst, so ret (inclusive) to lst (exclusive) is all
  488. the elements retained, making the equality search loop easy.
  489. If an item must be deleted, elements from keeplst (inclusive) to lst
  490. (exclusive) must be copied and appended to ret. When there's no more
  491. deletions, *p is left set to keeplst, so ret shares structure with the
  492. original lst. (ret will be the entire original lst if there are no
  493. deletions.) */
  494. /* skip to end if an empty list (or something invalid) */
  495. ret = SCM_EOL;
  496. if (SCM_UNBNDP (pred))
  497. equal_p = equal_trampoline;
  498. else
  499. {
  500. SCM_VALIDATE_PROC (SCM_ARG2, pred);
  501. equal_p = scm_call_2;
  502. }
  503. keeplst = lst;
  504. count = 0;
  505. p = &ret;
  506. for ( ; scm_is_pair (lst); lst = SCM_CDR (lst))
  507. {
  508. item = SCM_CAR (lst);
  509. /* look for item in "ret" list */
  510. for (l = ret; scm_is_pair (l); l = SCM_CDR (l))
  511. {
  512. if (scm_is_true (equal_p (pred, SCM_CAR (l), item)))
  513. {
  514. /* "item" is a duplicate, so copy keeplst onto ret */
  515. duplicate:
  516. p = list_copy_part (keeplst, count, p);
  517. keeplst = SCM_CDR (lst); /* elem after the one deleted */
  518. count = 0;
  519. goto next_elem;
  520. }
  521. }
  522. /* look for item in "keeplst" list
  523. be careful traversing, in case nasty code changed the cdrs */
  524. for (i = 0, l = keeplst;
  525. i < count && scm_is_pair (l);
  526. i++, l = SCM_CDR (l))
  527. if (scm_is_true (equal_p (pred, SCM_CAR (l), item)))
  528. goto duplicate;
  529. /* keep this element */
  530. count++;
  531. next_elem:
  532. ;
  533. }
  534. SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst), lst, SCM_ARG1, FUNC_NAME, "list");
  535. /* share tail of keeplst items */
  536. *p = keeplst;
  537. return ret;
  538. }
  539. #undef FUNC_NAME
  540. SCM_DEFINE (scm_srfi1_delete_duplicates_x, "delete-duplicates!", 1, 1, 0,
  541. (SCM lst, SCM pred),
  542. "Return a list containing the elements of @var{lst} but without\n"
  543. "duplicates.\n"
  544. "\n"
  545. "When elements are equal, only the first in @var{lst} is\n"
  546. "retained. Equal elements can be anywhere in @var{lst}, they\n"
  547. "don't have to be adjacent. The returned list will have the\n"
  548. "retained elements in the same order as they were in @var{lst}.\n"
  549. "\n"
  550. "Equality is determined by @var{pred}, or @code{equal?} if not\n"
  551. "given. Calls @code{(pred x y)} are made with element @var{x}\n"
  552. "being before @var{y} in @var{lst}. A call is made at most once\n"
  553. "for each combination, but the sequence of the calls across the\n"
  554. "elements is unspecified.\n"
  555. "\n"
  556. "@var{lst} may be modified to construct the returned list.\n"
  557. "\n"
  558. "In the worst case, this is an @math{O(N^2)} algorithm because\n"
  559. "it must check each element against all those preceding it. For\n"
  560. "long lists it is more efficient to sort and then compare only\n"
  561. "adjacent elements.")
  562. #define FUNC_NAME s_scm_srfi1_delete_duplicates_x
  563. {
  564. scm_t_trampoline_2 equal_p;
  565. SCM ret, endret, item, l;
  566. /* ret is the return list, constructed from the pairs in lst. endret is
  567. the last pair of ret, initially the first pair. lst is advanced as
  568. elements are considered. */
  569. /* skip to end if an empty list (or something invalid) */
  570. ret = lst;
  571. if (scm_is_pair (lst))
  572. {
  573. if (SCM_UNBNDP (pred))
  574. equal_p = equal_trampoline;
  575. else
  576. {
  577. SCM_VALIDATE_PROC (SCM_ARG2, pred);
  578. equal_p = scm_call_2;
  579. }
  580. endret = ret;
  581. /* loop over lst elements starting from second */
  582. for (;;)
  583. {
  584. lst = SCM_CDR (lst);
  585. if (! scm_is_pair (lst))
  586. break;
  587. item = SCM_CAR (lst);
  588. /* is item equal to any element from ret to endret (inclusive)? */
  589. l = ret;
  590. for (;;)
  591. {
  592. if (scm_is_true (equal_p (pred, SCM_CAR (l), item)))
  593. break; /* equal, forget this element */
  594. if (scm_is_eq (l, endret))
  595. {
  596. /* not equal to any, so append this pair */
  597. SCM_SETCDR (endret, lst);
  598. endret = lst;
  599. break;
  600. }
  601. l = SCM_CDR (l);
  602. }
  603. }
  604. /* terminate, in case last element was deleted */
  605. SCM_SETCDR (endret, SCM_EOL);
  606. }
  607. /* demand that lst was a proper list */
  608. SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst), lst, SCM_ARG1, FUNC_NAME, "list");
  609. return ret;
  610. }
  611. #undef FUNC_NAME
  612. SCM_DEFINE (scm_srfi1_drop_right, "drop-right", 2, 0, 0,
  613. (SCM lst, SCM n),
  614. "Return a new list containing all except the last @var{n}\n"
  615. "elements of @var{lst}.")
  616. #define FUNC_NAME s_scm_srfi1_drop_right
  617. {
  618. SCM tail = scm_list_tail (lst, n);
  619. SCM ret = SCM_EOL;
  620. SCM *rend = &ret;
  621. while (scm_is_pair (tail))
  622. {
  623. *rend = scm_cons (SCM_CAR (lst), SCM_EOL);
  624. rend = SCM_CDRLOC (*rend);
  625. lst = SCM_CDR (lst);
  626. tail = SCM_CDR (tail);
  627. }
  628. SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P(tail), tail, SCM_ARG1, FUNC_NAME, "list");
  629. return ret;
  630. }
  631. #undef FUNC_NAME
  632. SCM_DEFINE (scm_srfi1_drop_right_x, "drop-right!", 2, 0, 0,
  633. (SCM lst, SCM n),
  634. "Return the a list containing the @var{n} last elements of\n"
  635. "@var{lst}. @var{lst} may be modified to build the return.")
  636. #define FUNC_NAME s_scm_srfi1_drop_right_x
  637. {
  638. SCM tail, *p;
  639. if (scm_is_eq (n, SCM_INUM0))
  640. return lst;
  641. tail = scm_list_tail (lst, n);
  642. p = &lst;
  643. /* p and tail work along the list, p being the cdrloc of the cell n steps
  644. behind tail */
  645. for ( ; scm_is_pair (tail); tail = SCM_CDR (tail))
  646. p = SCM_CDRLOC (*p);
  647. SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P(tail), tail, SCM_ARG1, FUNC_NAME, "list");
  648. *p = SCM_EOL;
  649. return lst;
  650. }
  651. #undef FUNC_NAME
  652. SCM_DEFINE (scm_srfi1_drop_while, "drop-while", 2, 0, 0,
  653. (SCM pred, SCM lst),
  654. "Drop the longest initial prefix of @var{lst} whose elements all\n"
  655. "satisfy the predicate @var{pred}.")
  656. #define FUNC_NAME s_scm_srfi1_drop_while
  657. {
  658. SCM_VALIDATE_PROC (SCM_ARG1, pred);
  659. for ( ; scm_is_pair (lst); lst = SCM_CDR (lst))
  660. if (scm_is_false (scm_call_1 (pred, SCM_CAR (lst))))
  661. goto done;
  662. SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst), lst, SCM_ARG2, FUNC_NAME, "list");
  663. done:
  664. return lst;
  665. }
  666. #undef FUNC_NAME
  667. SCM_DEFINE (scm_srfi1_eighth, "eighth", 1, 0, 0,
  668. (SCM lst),
  669. "Return the eighth element of @var{lst}.")
  670. #define FUNC_NAME s_scm_srfi1_eighth
  671. {
  672. return scm_list_ref (lst, SCM_I_MAKINUM (7));
  673. }
  674. #undef FUNC_NAME
  675. SCM_DEFINE (scm_srfi1_fifth, "fifth", 1, 0, 0,
  676. (SCM lst),
  677. "Return the fifth element of @var{lst}.")
  678. #define FUNC_NAME s_scm_srfi1_fifth
  679. {
  680. return scm_list_ref (lst, SCM_I_MAKINUM (4));
  681. }
  682. #undef FUNC_NAME
  683. SCM_DEFINE (scm_srfi1_filter_map, "filter-map", 2, 0, 1,
  684. (SCM proc, SCM list1, SCM rest),
  685. "Apply @var{proc} to to the elements of @var{list1} @dots{} and\n"
  686. "return a list of the results as per SRFI-1 @code{map}, except\n"
  687. "that any @code{#f} results are omitted from the list returned.")
  688. #define FUNC_NAME s_scm_srfi1_filter_map
  689. {
  690. SCM ret, *loc, elem, newcell, lst;
  691. int argnum;
  692. SCM_VALIDATE_REST_ARGUMENT (rest);
  693. ret = SCM_EOL;
  694. loc = &ret;
  695. if (scm_is_null (rest))
  696. {
  697. /* one list */
  698. SCM_VALIDATE_PROC (SCM_ARG1, proc);
  699. for ( ; scm_is_pair (list1); list1 = SCM_CDR (list1))
  700. {
  701. elem = scm_call_1 (proc, SCM_CAR (list1));
  702. if (scm_is_true (elem))
  703. {
  704. newcell = scm_cons (elem, SCM_EOL);
  705. *loc = newcell;
  706. loc = SCM_CDRLOC (newcell);
  707. }
  708. }
  709. /* check below that list1 is a proper list, and done */
  710. end_list1:
  711. lst = list1;
  712. argnum = 2;
  713. }
  714. else if (scm_is_null (SCM_CDR (rest)))
  715. {
  716. /* two lists */
  717. SCM list2 = SCM_CAR (rest);
  718. SCM_VALIDATE_PROC (SCM_ARG1, proc);
  719. for (;;)
  720. {
  721. if (! scm_is_pair (list1))
  722. goto end_list1;
  723. if (! scm_is_pair (list2))
  724. {
  725. lst = list2;
  726. argnum = 3;
  727. goto check_lst_and_done;
  728. }
  729. elem = scm_call_2 (proc, SCM_CAR (list1), SCM_CAR (list2));
  730. if (scm_is_true (elem))
  731. {
  732. newcell = scm_cons (elem, SCM_EOL);
  733. *loc = newcell;
  734. loc = SCM_CDRLOC (newcell);
  735. }
  736. list1 = SCM_CDR (list1);
  737. list2 = SCM_CDR (list2);
  738. }
  739. }
  740. else
  741. {
  742. /* three or more lists */
  743. SCM vec, args, a;
  744. size_t len, i;
  745. /* vec is the list arguments */
  746. vec = scm_vector (scm_cons (list1, rest));
  747. len = SCM_SIMPLE_VECTOR_LENGTH (vec);
  748. /* args is the argument list to pass to proc, same length as vec,
  749. re-used for each call */
  750. args = scm_make_list (SCM_I_MAKINUM (len), SCM_UNDEFINED);
  751. for (;;)
  752. {
  753. /* first elem of each list in vec into args, and step those
  754. vec entries onto their next element */
  755. for (i = 0, a = args, argnum = 2;
  756. i < len;
  757. i++, a = SCM_CDR (a), argnum++)
  758. {
  759. lst = SCM_SIMPLE_VECTOR_REF (vec, i); /* list argument */
  760. if (! scm_is_pair (lst))
  761. goto check_lst_and_done;
  762. SCM_SETCAR (a, SCM_CAR (lst)); /* arg for proc */
  763. SCM_SIMPLE_VECTOR_SET (vec, i, SCM_CDR (lst)); /* rest of lst */
  764. }
  765. elem = scm_apply (proc, args, SCM_EOL);
  766. if (scm_is_true (elem))
  767. {
  768. newcell = scm_cons (elem, SCM_EOL);
  769. *loc = newcell;
  770. loc = SCM_CDRLOC (newcell);
  771. }
  772. }
  773. }
  774. check_lst_and_done:
  775. SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst), lst, argnum, FUNC_NAME, "list");
  776. return ret;
  777. }
  778. #undef FUNC_NAME
  779. SCM_DEFINE (scm_srfi1_find, "find", 2, 0, 0,
  780. (SCM pred, SCM lst),
  781. "Return the first element of @var{lst} which satisfies the\n"
  782. "predicate @var{pred}, or return @code{#f} if no such element is\n"
  783. "found.")
  784. #define FUNC_NAME s_scm_srfi1_find
  785. {
  786. SCM_VALIDATE_PROC (SCM_ARG1, pred);
  787. for ( ; scm_is_pair (lst); lst = SCM_CDR (lst))
  788. {
  789. SCM elem = SCM_CAR (lst);
  790. if (scm_is_true (scm_call_1 (pred, elem)))
  791. return elem;
  792. }
  793. SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst), lst, SCM_ARG2, FUNC_NAME, "list");
  794. return SCM_BOOL_F;
  795. }
  796. #undef FUNC_NAME
  797. SCM_DEFINE (scm_srfi1_find_tail, "find-tail", 2, 0, 0,
  798. (SCM pred, SCM lst),
  799. "Return the first pair of @var{lst} whose @sc{car} satisfies the\n"
  800. "predicate @var{pred}, or return @code{#f} if no such element is\n"
  801. "found.")
  802. #define FUNC_NAME s_scm_srfi1_find_tail
  803. {
  804. SCM_VALIDATE_PROC (SCM_ARG1, pred);
  805. for ( ; scm_is_pair (lst); lst = SCM_CDR (lst))
  806. if (scm_is_true (scm_call_1 (pred, SCM_CAR (lst))))
  807. return lst;
  808. SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst), lst, SCM_ARG2, FUNC_NAME, "list");
  809. return SCM_BOOL_F;
  810. }
  811. #undef FUNC_NAME
  812. SCM
  813. scm_srfi1_fold (SCM proc, SCM init, SCM list1, SCM rest)
  814. {
  815. CACHE_VAR (fold, "fold");
  816. return scm_apply_3 (fold, proc, init, list1, rest);
  817. }
  818. SCM
  819. scm_srfi1_last (SCM lst)
  820. {
  821. CACHE_VAR (last, "last");
  822. return scm_call_1 (last, lst);
  823. }
  824. SCM_DEFINE (scm_srfi1_length_plus, "length+", 1, 0, 0,
  825. (SCM lst),
  826. "Return the length of @var{lst}, or @code{#f} if @var{lst} is\n"
  827. "circular.")
  828. #define FUNC_NAME s_scm_srfi1_length_plus
  829. {
  830. long len = scm_ilength (lst);
  831. return (len >= 0 ? SCM_I_MAKINUM (len) : SCM_BOOL_F);
  832. }
  833. #undef FUNC_NAME
  834. SCM
  835. scm_srfi1_list_index (SCM pred, SCM list1, SCM rest)
  836. {
  837. CACHE_VAR (list_index, "list-index");
  838. return scm_apply_2 (list_index, pred, list1, rest);
  839. }
  840. /* This routine differs from the core list-copy in allowing improper lists.
  841. Maybe the core could allow them similarly. */
  842. SCM_DEFINE (scm_srfi1_list_copy, "list-copy", 1, 0, 0,
  843. (SCM lst),
  844. "Return a copy of the given list @var{lst}.\n"
  845. "\n"
  846. "@var{lst} can be a proper or improper list. And if @var{lst}\n"
  847. "is not a pair then it's treated as the final tail of an\n"
  848. "improper list and simply returned.")
  849. #define FUNC_NAME s_scm_srfi1_list_copy
  850. {
  851. SCM newlst;
  852. SCM * fill_here;
  853. SCM from_here;
  854. newlst = lst;
  855. fill_here = &newlst;
  856. from_here = lst;
  857. while (scm_is_pair (from_here))
  858. {
  859. SCM c;
  860. c = scm_cons (SCM_CAR (from_here), SCM_CDR (from_here));
  861. *fill_here = c;
  862. fill_here = SCM_CDRLOC (c);
  863. from_here = SCM_CDR (from_here);
  864. }
  865. return newlst;
  866. }
  867. #undef FUNC_NAME
  868. SCM
  869. scm_srfi1_list_tabulate (SCM n, SCM proc)
  870. {
  871. CACHE_VAR (list_tabulate, "list-tabulate");
  872. return scm_call_2 (list_tabulate, n, proc);
  873. }
  874. SCM_DEFINE (scm_srfi1_lset_adjoin, "lset-adjoin", 2, 0, 1,
  875. (SCM equal, SCM lst, SCM rest),
  876. "Add to @var{list} any of the given @var{elem}s not already in\n"
  877. "the list. @var{elem}s are @code{cons}ed onto the start of\n"
  878. "@var{list} (so the return shares a common tail with\n"
  879. "@var{list}), but the order they're added is unspecified.\n"
  880. "\n"
  881. "The given @var{=} procedure is used for comparing elements,\n"
  882. "called as @code{(@var{=} listelem elem)}, ie.@: the second\n"
  883. "argument is one of the given @var{elem} parameters.\n"
  884. "\n"
  885. "@example\n"
  886. "(lset-adjoin eqv? '(1 2 3) 4 1 5) @result{} (5 4 1 2 3)\n"
  887. "@end example")
  888. #define FUNC_NAME s_scm_srfi1_lset_adjoin
  889. {
  890. SCM l, elem;
  891. SCM_VALIDATE_PROC (SCM_ARG1, equal);
  892. SCM_VALIDATE_REST_ARGUMENT (rest);
  893. /* It's not clear if duplicates among the `rest' elements are meant to be
  894. cast out. The spec says `=' is called as (= list-elem rest-elem),
  895. suggesting perhaps not, but the reference implementation shows the
  896. "list" at each stage as including those "rest" elements already added.
  897. The latter corresponds to what's described for lset-union, so that's
  898. what's done here. */
  899. for ( ; scm_is_pair (rest); rest = SCM_CDR (rest))
  900. {
  901. elem = SCM_CAR (rest);
  902. for (l = lst; scm_is_pair (l); l = SCM_CDR (l))
  903. if (scm_is_true (scm_call_2 (equal, SCM_CAR (l), elem)))
  904. goto next_elem; /* elem already in lst, don't add */
  905. SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P(l), lst, SCM_ARG2, FUNC_NAME, "list");
  906. /* elem is not equal to anything already in lst, add it */
  907. lst = scm_cons (elem, lst);
  908. next_elem:
  909. ;
  910. }
  911. return lst;
  912. }
  913. #undef FUNC_NAME
  914. SCM_DEFINE (scm_srfi1_lset_difference_x, "lset-difference!", 2, 0, 1,
  915. (SCM equal, SCM lst, SCM rest),
  916. "Return @var{lst} with any elements in the lists in @var{rest}\n"
  917. "removed (ie.@: subtracted). For only one @var{lst} argument,\n"
  918. "just that list is returned.\n"
  919. "\n"
  920. "The given @var{equal} procedure is used for comparing elements,\n"
  921. "called as @code{(@var{equal} elem1 elemN)}. The first argument\n"
  922. "is from @var{lst} and the second from one of the subsequent\n"
  923. "lists. But exactly which calls are made and in what order is\n"
  924. "unspecified.\n"
  925. "\n"
  926. "@example\n"
  927. "(lset-difference! eqv? (list 'x 'y)) @result{} (x y)\n"
  928. "(lset-difference! eqv? (list 1 2 3) '(3 1)) @result{} (2)\n"
  929. "(lset-difference! eqv? (list 1 2 3) '(3) '(2)) @result{} (1)\n"
  930. "@end example\n"
  931. "\n"
  932. "@code{lset-difference!} may modify @var{lst} to form its\n"
  933. "result.")
  934. #define FUNC_NAME s_scm_srfi1_lset_difference_x
  935. {
  936. SCM ret, *pos, elem, r, b;
  937. int argnum;
  938. SCM_VALIDATE_PROC (SCM_ARG1, equal);
  939. SCM_VALIDATE_REST_ARGUMENT (rest);
  940. ret = SCM_EOL;
  941. pos = &ret;
  942. for ( ; scm_is_pair (lst); lst = SCM_CDR (lst))
  943. {
  944. elem = SCM_CAR (lst);
  945. for (r = rest, argnum = SCM_ARG3;
  946. scm_is_pair (r);
  947. r = SCM_CDR (r), argnum++)
  948. {
  949. for (b = SCM_CAR (r); scm_is_pair (b); b = SCM_CDR (b))
  950. if (scm_is_true (scm_call_2 (equal, elem, SCM_CAR (b))))
  951. goto next_elem; /* equal to elem, so drop that elem */
  952. SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (b), b, argnum, FUNC_NAME,"list");
  953. }
  954. /* elem not equal to anything in later lists, so keep it */
  955. *pos = lst;
  956. pos = SCM_CDRLOC (lst);
  957. next_elem:
  958. ;
  959. }
  960. SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst), lst, SCM_ARG2, FUNC_NAME, "list");
  961. *pos = SCM_EOL;
  962. return ret;
  963. }
  964. #undef FUNC_NAME
  965. /* Typechecking for multi-argument MAP and FOR-EACH.
  966. Verify that each element of the vector ARGV, except for the first,
  967. is a list and return minimum length. Attribute errors to WHO,
  968. and claim that the i'th element of ARGV is WHO's i+2'th argument. */
  969. static inline int
  970. check_map_args (SCM argv,
  971. long len,
  972. SCM gf,
  973. SCM proc,
  974. SCM args,
  975. const char *who)
  976. {
  977. long i;
  978. SCM elt;
  979. for (i = SCM_SIMPLE_VECTOR_LENGTH (argv) - 1; i >= 1; i--)
  980. {
  981. long elt_len;
  982. elt = SCM_SIMPLE_VECTOR_REF (argv, i);
  983. if (!(scm_is_null (elt) || scm_is_pair (elt)))
  984. goto check_map_error;
  985. elt_len = srfi1_ilength (elt);
  986. if (elt_len < -1)
  987. goto check_map_error;
  988. if (len < 0 || (elt_len >= 0 && elt_len < len))
  989. len = elt_len;
  990. }
  991. if (len < 0)
  992. {
  993. /* i == 0 */
  994. elt = SCM_EOL;
  995. check_map_error:
  996. if (gf)
  997. scm_apply_generic (gf, scm_cons (proc, args));
  998. else
  999. scm_wrong_type_arg (who, i + 2, elt);
  1000. }
  1001. scm_remember_upto_here_1 (argv);
  1002. return len;
  1003. }
  1004. SCM_GPROC (s_srfi1_map, "map", 2, 0, 1, scm_srfi1_map, g_srfi1_map);
  1005. /* Note: Currently, scm_srfi1_map applies PROC to the argument list(s)
  1006. sequentially, starting with the first element(s). This is used in
  1007. the Scheme procedure `map-in-order', which guarantees sequential
  1008. behaviour, is implemented using scm_map. If the behaviour changes,
  1009. we need to update `map-in-order'.
  1010. */
  1011. SCM
  1012. scm_srfi1_map (SCM proc, SCM arg1, SCM args)
  1013. #define FUNC_NAME s_srfi1_map
  1014. {
  1015. long i, len;
  1016. SCM res = SCM_EOL;
  1017. SCM *pres = &res;
  1018. len = srfi1_ilength (arg1);
  1019. SCM_GASSERTn ((scm_is_null (arg1) || scm_is_pair (arg1)) && len >= -1,
  1020. g_srfi1_map,
  1021. scm_cons2 (proc, arg1, args), SCM_ARG2, s_srfi1_map);
  1022. SCM_VALIDATE_REST_ARGUMENT (args);
  1023. if (scm_is_null (args))
  1024. {
  1025. SCM_GASSERT2 (scm_is_true (scm_procedure_p (proc)), g_srfi1_map,
  1026. proc, arg1, SCM_ARG1, s_srfi1_map);
  1027. SCM_GASSERT2 (len >= 0, g_srfi1_map, proc, arg1, SCM_ARG2, s_srfi1_map);
  1028. while (SCM_NIMP (arg1))
  1029. {
  1030. *pres = scm_list_1 (scm_call_1 (proc, SCM_CAR (arg1)));
  1031. pres = SCM_CDRLOC (*pres);
  1032. arg1 = SCM_CDR (arg1);
  1033. }
  1034. return res;
  1035. }
  1036. if (scm_is_null (SCM_CDR (args)))
  1037. {
  1038. SCM arg2 = SCM_CAR (args);
  1039. int len2 = srfi1_ilength (arg2);
  1040. SCM_GASSERTn (scm_is_true (scm_procedure_p (proc)), g_srfi1_map,
  1041. scm_cons2 (proc, arg1, args), SCM_ARG1, s_srfi1_map);
  1042. if (len < 0 || (len2 >= 0 && len2 < len))
  1043. len = len2;
  1044. SCM_GASSERTn ((scm_is_null (arg2) || scm_is_pair (arg2))
  1045. && len >= 0 && len2 >= -1,
  1046. g_srfi1_map,
  1047. scm_cons2 (proc, arg1, args),
  1048. len2 >= 0 ? SCM_ARG2 : SCM_ARG3,
  1049. s_srfi1_map);
  1050. while (len > 0)
  1051. {
  1052. *pres = scm_list_1 (scm_call_2 (proc, SCM_CAR (arg1), SCM_CAR (arg2)));
  1053. pres = SCM_CDRLOC (*pres);
  1054. arg1 = SCM_CDR (arg1);
  1055. arg2 = SCM_CDR (arg2);
  1056. --len;
  1057. }
  1058. return res;
  1059. }
  1060. args = scm_vector (arg1 = scm_cons (arg1, args));
  1061. len = check_map_args (args, len, g_srfi1_map, proc, arg1, s_srfi1_map);
  1062. while (len > 0)
  1063. {
  1064. arg1 = SCM_EOL;
  1065. for (i = SCM_SIMPLE_VECTOR_LENGTH (args) - 1; i >= 0; i--)
  1066. {
  1067. SCM elt = SCM_SIMPLE_VECTOR_REF (args, i);
  1068. arg1 = scm_cons (SCM_CAR (elt), arg1);
  1069. SCM_SIMPLE_VECTOR_SET (args, i, SCM_CDR (elt));
  1070. }
  1071. *pres = scm_list_1 (scm_apply (proc, arg1, SCM_EOL));
  1072. pres = SCM_CDRLOC (*pres);
  1073. --len;
  1074. }
  1075. return res;
  1076. }
  1077. #undef FUNC_NAME
  1078. SCM_REGISTER_PROC (s_srfi1_map_in_order, "map-in-order", 2, 0, 1, scm_srfi1_map);
  1079. SCM_GPROC (s_srfi1_for_each, "for-each", 2, 0, 1, scm_srfi1_for_each, g_srfi1_for_each);
  1080. SCM
  1081. scm_srfi1_for_each (SCM proc, SCM arg1, SCM args)
  1082. #define FUNC_NAME s_srfi1_for_each
  1083. {
  1084. long i, len;
  1085. len = srfi1_ilength (arg1);
  1086. SCM_GASSERTn ((scm_is_null (arg1) || scm_is_pair (arg1)) && len >= -1,
  1087. g_srfi1_for_each, scm_cons2 (proc, arg1, args),
  1088. SCM_ARG2, s_srfi1_for_each);
  1089. SCM_VALIDATE_REST_ARGUMENT (args);
  1090. if (scm_is_null (args))
  1091. {
  1092. SCM_GASSERT2 (scm_is_true (scm_procedure_p (proc)), g_srfi1_for_each,
  1093. proc, arg1, SCM_ARG1, s_srfi1_for_each);
  1094. SCM_GASSERT2 (len >= 0, g_srfi1_for_each, proc, arg1,
  1095. SCM_ARG2, s_srfi1_map);
  1096. while (SCM_NIMP (arg1))
  1097. {
  1098. scm_call_1 (proc, SCM_CAR (arg1));
  1099. arg1 = SCM_CDR (arg1);
  1100. }
  1101. return SCM_UNSPECIFIED;
  1102. }
  1103. if (scm_is_null (SCM_CDR (args)))
  1104. {
  1105. SCM arg2 = SCM_CAR (args);
  1106. int len2 = srfi1_ilength (arg2);
  1107. SCM_GASSERTn (scm_is_true (scm_procedure_p (proc)), g_srfi1_for_each,
  1108. scm_cons2 (proc, arg1, args), SCM_ARG1, s_srfi1_for_each);
  1109. if (len < 0 || (len2 >= 0 && len2 < len))
  1110. len = len2;
  1111. SCM_GASSERTn ((scm_is_null (arg2) || scm_is_pair (arg2))
  1112. && len >= 0 && len2 >= -1,
  1113. g_srfi1_for_each,
  1114. scm_cons2 (proc, arg1, args),
  1115. len2 >= 0 ? SCM_ARG2 : SCM_ARG3,
  1116. s_srfi1_for_each);
  1117. while (len > 0)
  1118. {
  1119. scm_call_2 (proc, SCM_CAR (arg1), SCM_CAR (arg2));
  1120. arg1 = SCM_CDR (arg1);
  1121. arg2 = SCM_CDR (arg2);
  1122. --len;
  1123. }
  1124. return SCM_UNSPECIFIED;
  1125. }
  1126. args = scm_vector (arg1 = scm_cons (arg1, args));
  1127. len = check_map_args (args, len, g_srfi1_for_each, proc, arg1,
  1128. s_srfi1_for_each);
  1129. while (len > 0)
  1130. {
  1131. arg1 = SCM_EOL;
  1132. for (i = SCM_SIMPLE_VECTOR_LENGTH (args) - 1; i >= 0; i--)
  1133. {
  1134. SCM elt = SCM_SIMPLE_VECTOR_REF (args, i);
  1135. arg1 = scm_cons (SCM_CAR (elt), arg1);
  1136. SCM_SIMPLE_VECTOR_SET (args, i, SCM_CDR (elt));
  1137. }
  1138. scm_apply (proc, arg1, SCM_EOL);
  1139. --len;
  1140. }
  1141. return SCM_UNSPECIFIED;
  1142. }
  1143. #undef FUNC_NAME
  1144. SCM_DEFINE (scm_srfi1_member, "member", 2, 1, 0,
  1145. (SCM x, SCM lst, SCM pred),
  1146. "Return the first sublist of @var{lst} whose @sc{car} is equal\n"
  1147. "to @var{x}. If @var{x} does not appear in @var{lst}, return\n"
  1148. "@code{#f}.\n"
  1149. "\n"
  1150. "Equality is determined by @code{equal?}, or by the equality\n"
  1151. "predicate @var{=} if given. @var{=} is called @code{(= @var{x}\n"
  1152. "elem)}, ie.@: with the given @var{x} first, so for example to\n"
  1153. "find the first element greater than 5,\n"
  1154. "\n"
  1155. "@example\n"
  1156. "(member 5 '(3 5 1 7 2 9) <) @result{} (7 2 9)\n"
  1157. "@end example\n"
  1158. "\n"
  1159. "This version of @code{member} extends the core @code{member} by\n"
  1160. "accepting an equality predicate.")
  1161. #define FUNC_NAME s_scm_srfi1_member
  1162. {
  1163. scm_t_trampoline_2 equal_p;
  1164. SCM_VALIDATE_LIST (2, lst);
  1165. if (SCM_UNBNDP (pred))
  1166. equal_p = equal_trampoline;
  1167. else
  1168. {
  1169. SCM_VALIDATE_PROC (SCM_ARG3, pred);
  1170. equal_p = scm_call_2;
  1171. }
  1172. for (; !SCM_NULL_OR_NIL_P (lst); lst = SCM_CDR (lst))
  1173. {
  1174. if (scm_is_true (equal_p (pred, x, SCM_CAR (lst))))
  1175. return lst;
  1176. }
  1177. return SCM_BOOL_F;
  1178. }
  1179. #undef FUNC_NAME
  1180. SCM_DEFINE (scm_srfi1_assoc, "assoc", 2, 1, 0,
  1181. (SCM key, SCM alist, SCM pred),
  1182. "Behaves like @code{assq} but uses third argument @var{pred?}\n"
  1183. "for key comparison. If @var{pred?} is not supplied,\n"
  1184. "@code{equal?} is used. (Extended from R5RS.)\n")
  1185. #define FUNC_NAME s_scm_srfi1_assoc
  1186. {
  1187. SCM ls = alist;
  1188. scm_t_trampoline_2 equal_p;
  1189. if (SCM_UNBNDP (pred))
  1190. equal_p = equal_trampoline;
  1191. else
  1192. {
  1193. SCM_VALIDATE_PROC (SCM_ARG3, pred);
  1194. equal_p = scm_call_2;
  1195. }
  1196. for(; scm_is_pair (ls); ls = SCM_CDR (ls))
  1197. {
  1198. SCM tmp = SCM_CAR (ls);
  1199. SCM_ASSERT_TYPE (scm_is_pair (tmp), alist, SCM_ARG2, FUNC_NAME,
  1200. "association list");
  1201. if (scm_is_true (equal_p (pred, key, SCM_CAR (tmp))))
  1202. return tmp;
  1203. }
  1204. SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (ls), alist, SCM_ARG2, FUNC_NAME,
  1205. "association list");
  1206. return SCM_BOOL_F;
  1207. }
  1208. #undef FUNC_NAME
  1209. SCM_DEFINE (scm_srfi1_ninth, "ninth", 1, 0, 0,
  1210. (SCM lst),
  1211. "Return the ninth element of @var{lst}.")
  1212. #define FUNC_NAME s_scm_srfi1_ninth
  1213. {
  1214. return scm_list_ref (lst, scm_from_int (8));
  1215. }
  1216. #undef FUNC_NAME
  1217. SCM
  1218. scm_srfi1_not_pair_p (SCM obj)
  1219. {
  1220. CACHE_VAR (not_pair_p, "not-pair?");
  1221. return scm_call_1 (not_pair_p, obj);
  1222. }
  1223. SCM_DEFINE (scm_srfi1_partition, "partition", 2, 0, 0,
  1224. (SCM pred, SCM list),
  1225. "Partition the elements of @var{list} with predicate @var{pred}.\n"
  1226. "Return two values: the list of elements satifying @var{pred} and\n"
  1227. "the list of elements @emph{not} satisfying @var{pred}. The order\n"
  1228. "of the output lists follows the order of @var{list}. @var{list}\n"
  1229. "is not mutated. One of the output lists may share memory with @var{list}.\n")
  1230. #define FUNC_NAME s_scm_srfi1_partition
  1231. {
  1232. /* In this implementation, the output lists don't share memory with
  1233. list, because it's probably not worth the effort. */
  1234. SCM orig_list = list;
  1235. SCM kept = scm_cons(SCM_EOL, SCM_EOL);
  1236. SCM kept_tail = kept;
  1237. SCM dropped = scm_cons(SCM_EOL, SCM_EOL);
  1238. SCM dropped_tail = dropped;
  1239. SCM_VALIDATE_PROC (SCM_ARG1, pred);
  1240. for (; !SCM_NULL_OR_NIL_P (list); list = SCM_CDR(list)) {
  1241. SCM elt, new_tail;
  1242. /* Make sure LIST is not a dotted list. */
  1243. SCM_ASSERT (scm_is_pair (list), orig_list, SCM_ARG2, FUNC_NAME);
  1244. elt = SCM_CAR (list);
  1245. new_tail = scm_cons (SCM_CAR (list), SCM_EOL);
  1246. if (scm_is_true (scm_call_1 (pred, elt))) {
  1247. SCM_SETCDR(kept_tail, new_tail);
  1248. kept_tail = new_tail;
  1249. }
  1250. else {
  1251. SCM_SETCDR(dropped_tail, new_tail);
  1252. dropped_tail = new_tail;
  1253. }
  1254. }
  1255. /* re-use the initial conses for the values list */
  1256. SCM_SETCAR(kept, SCM_CDR(kept));
  1257. SCM_SETCDR(kept, dropped);
  1258. SCM_SETCAR(dropped, SCM_CDR(dropped));
  1259. SCM_SETCDR(dropped, SCM_EOL);
  1260. return scm_values(kept);
  1261. }
  1262. #undef FUNC_NAME
  1263. SCM_DEFINE (scm_srfi1_partition_x, "partition!", 2, 0, 0,
  1264. (SCM pred, SCM lst),
  1265. "Split @var{lst} into those elements which do and don't satisfy\n"
  1266. "the predicate @var{pred}.\n"
  1267. "\n"
  1268. "The return is two values (@pxref{Multiple Values}), the first\n"
  1269. "being a list of all elements from @var{lst} which satisfy\n"
  1270. "@var{pred}, the second a list of those which do not.\n"
  1271. "\n"
  1272. "The elements in the result lists are in the same order as in\n"
  1273. "@var{lst} but the order in which the calls @code{(@var{pred}\n"
  1274. "elem)} are made on the list elements is unspecified.\n"
  1275. "\n"
  1276. "@var{lst} may be modified to construct the return lists.")
  1277. #define FUNC_NAME s_scm_srfi1_partition_x
  1278. {
  1279. SCM tlst, flst, *tp, *fp;
  1280. SCM_ASSERT (scm_is_true (scm_procedure_p (pred)), pred, SCM_ARG1, FUNC_NAME);
  1281. /* tlst and flst are the lists of true and false elements. tp and fp are
  1282. where to store to append to them, initially &tlst and &flst, then
  1283. SCM_CDRLOC of the last pair in the respective lists. */
  1284. tlst = SCM_EOL;
  1285. flst = SCM_EOL;
  1286. tp = &tlst;
  1287. fp = &flst;
  1288. for ( ; scm_is_pair (lst); lst = SCM_CDR (lst))
  1289. {
  1290. if (scm_is_true (scm_call_1 (pred, SCM_CAR (lst))))
  1291. {
  1292. *tp = lst;
  1293. tp = SCM_CDRLOC (lst);
  1294. }
  1295. else
  1296. {
  1297. *fp = lst;
  1298. fp = SCM_CDRLOC (lst);
  1299. }
  1300. }
  1301. SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst), lst, SCM_ARG2, FUNC_NAME, "list");
  1302. /* terminate whichever didn't get the last element(s) */
  1303. *tp = SCM_EOL;
  1304. *fp = SCM_EOL;
  1305. return scm_values (scm_list_2 (tlst, flst));
  1306. }
  1307. #undef FUNC_NAME
  1308. SCM_DEFINE (scm_srfi1_reduce, "reduce", 3, 0, 0,
  1309. (SCM proc, SCM def, SCM lst),
  1310. "@code{reduce} is a variant of @code{fold}, where the first call\n"
  1311. "to @var{proc} is on two elements from @var{lst}, rather than\n"
  1312. "one element and a given initial value.\n"
  1313. "\n"
  1314. "If @var{lst} is empty, @code{reduce} returns @var{def} (this is\n"
  1315. "the only use for @var{def}). If @var{lst} has just one element\n"
  1316. "then that's the return value. Otherwise @var{proc} is called\n"
  1317. "on the elements of @var{lst}.\n"
  1318. "\n"
  1319. "Each @var{proc} call is @code{(@var{proc} @var{elem}\n"
  1320. "@var{previous})}, where @var{elem} is from @var{lst} (the\n"
  1321. "second and subsequent elements of @var{lst}), and\n"
  1322. "@var{previous} is the return from the previous call to\n"
  1323. "@var{proc}. The first element of @var{lst} is the\n"
  1324. "@var{previous} for the first call to @var{proc}.\n"
  1325. "\n"
  1326. "For example, the following adds a list of numbers, the calls\n"
  1327. "made to @code{+} are shown. (Of course @code{+} accepts\n"
  1328. "multiple arguments and can add a list directly, with\n"
  1329. "@code{apply}.)\n"
  1330. "\n"
  1331. "@example\n"
  1332. "(reduce + 0 '(5 6 7)) @result{} 18\n"
  1333. "\n"
  1334. "(+ 6 5) @result{} 11\n"
  1335. "(+ 7 11) @result{} 18\n"
  1336. "@end example\n"
  1337. "\n"
  1338. "@code{reduce} can be used instead of @code{fold} where the\n"
  1339. "@var{init} value is an ``identity'', meaning a value which\n"
  1340. "under @var{proc} doesn't change the result, in this case 0 is\n"
  1341. "an identity since @code{(+ 5 0)} is just 5. @code{reduce}\n"
  1342. "avoids that unnecessary call.")
  1343. #define FUNC_NAME s_scm_srfi1_reduce
  1344. {
  1345. SCM ret;
  1346. SCM_VALIDATE_PROC (SCM_ARG1, proc);
  1347. ret = def; /* if lst is empty */
  1348. if (scm_is_pair (lst))
  1349. {
  1350. ret = SCM_CAR (lst); /* if lst has one element */
  1351. for (lst = SCM_CDR (lst); scm_is_pair (lst); lst = SCM_CDR (lst))
  1352. ret = scm_call_2 (proc, SCM_CAR (lst), ret);
  1353. }
  1354. SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst), lst, SCM_ARG3, FUNC_NAME, "list");
  1355. return ret;
  1356. }
  1357. #undef FUNC_NAME
  1358. SCM_DEFINE (scm_srfi1_reduce_right, "reduce-right", 3, 0, 0,
  1359. (SCM proc, SCM def, SCM lst),
  1360. "@code{reduce-right} is a variant of @code{fold-right}, where\n"
  1361. "the first call to @var{proc} is on two elements from @var{lst},\n"
  1362. "rather than one element and a given initial value.\n"
  1363. "\n"
  1364. "If @var{lst} is empty, @code{reduce-right} returns @var{def}\n"
  1365. "(this is the only use for @var{def}). If @var{lst} has just\n"
  1366. "one element then that's the return value. Otherwise @var{proc}\n"
  1367. "is called on the elements of @var{lst}.\n"
  1368. "\n"
  1369. "Each @var{proc} call is @code{(@var{proc} @var{elem}\n"
  1370. "@var{previous})}, where @var{elem} is from @var{lst} (the\n"
  1371. "second last and then working back to the first element of\n"
  1372. "@var{lst}), and @var{previous} is the return from the previous\n"
  1373. "call to @var{proc}. The last element of @var{lst} is the\n"
  1374. "@var{previous} for the first call to @var{proc}.\n"
  1375. "\n"
  1376. "For example, the following adds a list of numbers, the calls\n"
  1377. "made to @code{+} are shown. (Of course @code{+} accepts\n"
  1378. "multiple arguments and can add a list directly, with\n"
  1379. "@code{apply}.)\n"
  1380. "\n"
  1381. "@example\n"
  1382. "(reduce-right + 0 '(5 6 7)) @result{} 18\n"
  1383. "\n"
  1384. "(+ 6 7) @result{} 13\n"
  1385. "(+ 5 13) @result{} 18\n"
  1386. "@end example\n"
  1387. "\n"
  1388. "@code{reduce-right} can be used instead of @code{fold-right}\n"
  1389. "where the @var{init} value is an ``identity'', meaning a value\n"
  1390. "which under @var{proc} doesn't change the result, in this case\n"
  1391. "0 is an identity since @code{(+ 7 0)} is just 5.\n"
  1392. "@code{reduce-right} avoids that unnecessary call.\n"
  1393. "\n"
  1394. "@code{reduce} should be preferred over @code{reduce-right} if\n"
  1395. "the order of processing doesn't matter, or can be arranged\n"
  1396. "either way, since @code{reduce} is a little more efficient.")
  1397. #define FUNC_NAME s_scm_srfi1_reduce_right
  1398. {
  1399. /* To work backwards across a list requires either repeatedly traversing
  1400. to get each previous element, or using some memory for a reversed or
  1401. random-access form. Repeated traversal might not be too terrible, but
  1402. is of course quadratic complexity and hence to be avoided in case LST
  1403. is long. A vector is preferred over a reversed list since it's more
  1404. compact and is less work for the gc to collect. */
  1405. SCM vec, ret;
  1406. ssize_t len, i;
  1407. SCM_VALIDATE_PROC (SCM_ARG1, proc);
  1408. if (SCM_NULL_OR_NIL_P (lst))
  1409. return def;
  1410. vec = scm_vector (lst);
  1411. len = SCM_SIMPLE_VECTOR_LENGTH (vec);
  1412. ret = SCM_SIMPLE_VECTOR_REF (vec, len-1);
  1413. for (i = len-2; i >= 0; i--)
  1414. ret = scm_call_2 (proc, SCM_SIMPLE_VECTOR_REF (vec, i), ret);
  1415. return ret;
  1416. }
  1417. #undef FUNC_NAME
  1418. SCM_DEFINE (scm_srfi1_remove, "remove", 2, 0, 0,
  1419. (SCM pred, SCM list),
  1420. "Return a list containing all elements from @var{lst} which do\n"
  1421. "not satisfy the predicate @var{pred}. The elements in the\n"
  1422. "result list have the same order as in @var{lst}. The order in\n"
  1423. "which @var{pred} is applied to the list elements is not\n"
  1424. "specified.")
  1425. #define FUNC_NAME s_scm_srfi1_remove
  1426. {
  1427. SCM walk;
  1428. SCM *prev;
  1429. SCM res = SCM_EOL;
  1430. SCM_VALIDATE_PROC (SCM_ARG1, pred);
  1431. SCM_VALIDATE_LIST (2, list);
  1432. for (prev = &res, walk = list;
  1433. scm_is_pair (walk);
  1434. walk = SCM_CDR (walk))
  1435. {
  1436. if (scm_is_false (scm_call_1 (pred, SCM_CAR (walk))))
  1437. {
  1438. *prev = scm_cons (SCM_CAR (walk), SCM_EOL);
  1439. prev = SCM_CDRLOC (*prev);
  1440. }
  1441. }
  1442. return res;
  1443. }
  1444. #undef FUNC_NAME
  1445. SCM_DEFINE (scm_srfi1_remove_x, "remove!", 2, 0, 0,
  1446. (SCM pred, SCM list),
  1447. "Return a list containing all elements from @var{list} which do\n"
  1448. "not satisfy the predicate @var{pred}. The elements in the\n"
  1449. "result list have the same order as in @var{list}. The order in\n"
  1450. "which @var{pred} is applied to the list elements is not\n"
  1451. "specified. @var{list} may be modified to build the return\n"
  1452. "list.")
  1453. #define FUNC_NAME s_scm_srfi1_remove_x
  1454. {
  1455. SCM walk;
  1456. SCM *prev;
  1457. SCM_VALIDATE_PROC (SCM_ARG1, pred);
  1458. SCM_VALIDATE_LIST (2, list);
  1459. for (prev = &list, walk = list;
  1460. scm_is_pair (walk);
  1461. walk = SCM_CDR (walk))
  1462. {
  1463. if (scm_is_false (scm_call_1 (pred, SCM_CAR (walk))))
  1464. prev = SCM_CDRLOC (walk);
  1465. else
  1466. *prev = SCM_CDR (walk);
  1467. }
  1468. return list;
  1469. }
  1470. #undef FUNC_NAME
  1471. SCM_DEFINE (scm_srfi1_seventh, "seventh", 1, 0, 0,
  1472. (SCM lst),
  1473. "Return the seventh element of @var{lst}.")
  1474. #define FUNC_NAME s_scm_srfi1_seventh
  1475. {
  1476. return scm_list_ref (lst, scm_from_int (6));
  1477. }
  1478. #undef FUNC_NAME
  1479. SCM_DEFINE (scm_srfi1_sixth, "sixth", 1, 0, 0,
  1480. (SCM lst),
  1481. "Return the sixth element of @var{lst}.")
  1482. #define FUNC_NAME s_scm_srfi1_sixth
  1483. {
  1484. return scm_list_ref (lst, scm_from_int (5));
  1485. }
  1486. #undef FUNC_NAME
  1487. SCM_DEFINE (scm_srfi1_span, "span", 2, 0, 0,
  1488. (SCM pred, SCM lst),
  1489. "Return two values, the longest initial prefix of @var{lst}\n"
  1490. "whose elements all satisfy the predicate @var{pred}, and the\n"
  1491. "remainder of @var{lst}.")
  1492. #define FUNC_NAME s_scm_srfi1_span
  1493. {
  1494. SCM ret, *p;
  1495. SCM_ASSERT (scm_is_true (scm_procedure_p (pred)), pred, SCM_ARG1, FUNC_NAME);
  1496. ret = SCM_EOL;
  1497. p = &ret;
  1498. for ( ; scm_is_pair (lst); lst = SCM_CDR (lst))
  1499. {
  1500. SCM elem = SCM_CAR (lst);
  1501. if (scm_is_false (scm_call_1 (pred, elem)))
  1502. goto done;
  1503. /* want this elem, tack it onto the end of ret */
  1504. *p = scm_cons (elem, SCM_EOL);
  1505. p = SCM_CDRLOC (*p);
  1506. }
  1507. SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst), lst, SCM_ARG2, FUNC_NAME, "list");
  1508. done:
  1509. return scm_values (scm_list_2 (ret, lst));
  1510. }
  1511. #undef FUNC_NAME
  1512. SCM_DEFINE (scm_srfi1_span_x, "span!", 2, 0, 0,
  1513. (SCM pred, SCM lst),
  1514. "Return two values, the longest initial prefix of @var{lst}\n"
  1515. "whose elements all satisfy the predicate @var{pred}, and the\n"
  1516. "remainder of @var{lst}. @var{lst} may be modified to form the\n"
  1517. "return.")
  1518. #define FUNC_NAME s_scm_srfi1_span_x
  1519. {
  1520. SCM upto, *p;
  1521. SCM_ASSERT (scm_is_true (scm_procedure_p (pred)), pred, SCM_ARG1, FUNC_NAME);
  1522. p = &lst;
  1523. for (upto = lst; scm_is_pair (upto); upto = SCM_CDR (upto))
  1524. {
  1525. if (scm_is_false (scm_call_1 (pred, SCM_CAR (upto))))
  1526. goto done;
  1527. /* want this element */
  1528. p = SCM_CDRLOC (upto);
  1529. }
  1530. SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (upto), lst, SCM_ARG2, FUNC_NAME, "list");
  1531. done:
  1532. *p = SCM_EOL;
  1533. return scm_values (scm_list_2 (lst, upto));
  1534. }
  1535. #undef FUNC_NAME
  1536. SCM_DEFINE (scm_srfi1_split_at, "split-at", 2, 0, 0,
  1537. (SCM lst, SCM n),
  1538. "Return two values (multiple values), being a list of the\n"
  1539. "elements before index @var{n} in @var{lst}, and a list of those\n"
  1540. "after.")
  1541. #define FUNC_NAME s_scm_srfi1_split_at
  1542. {
  1543. size_t nn;
  1544. /* pre is a list of elements before the i split point, loc is the CDRLOC
  1545. of the last cell, ie. where to store to append to it */
  1546. SCM pre = SCM_EOL;
  1547. SCM *loc = &pre;
  1548. for (nn = scm_to_size_t (n); nn != 0; nn--)
  1549. {
  1550. SCM_VALIDATE_CONS (SCM_ARG1, lst);
  1551. *loc = scm_cons (SCM_CAR (lst), SCM_EOL);
  1552. loc = SCM_CDRLOC (*loc);
  1553. lst = SCM_CDR(lst);
  1554. }
  1555. return scm_values (scm_list_2 (pre, lst));
  1556. }
  1557. #undef FUNC_NAME
  1558. SCM_DEFINE (scm_srfi1_split_at_x, "split-at!", 2, 0, 0,
  1559. (SCM lst, SCM n),
  1560. "Return two values (multiple values), being a list of the\n"
  1561. "elements before index @var{n} in @var{lst}, and a list of those\n"
  1562. "after. @var{lst} is modified to form those values.")
  1563. #define FUNC_NAME s_scm_srfi1_split_at
  1564. {
  1565. size_t nn;
  1566. SCM upto = lst;
  1567. SCM *loc = &lst;
  1568. for (nn = scm_to_size_t (n); nn != 0; nn--)
  1569. {
  1570. SCM_VALIDATE_CONS (SCM_ARG1, upto);
  1571. loc = SCM_CDRLOC (upto);
  1572. upto = SCM_CDR (upto);
  1573. }
  1574. *loc = SCM_EOL;
  1575. return scm_values (scm_list_2 (lst, upto));
  1576. }
  1577. #undef FUNC_NAME
  1578. SCM_DEFINE (scm_srfi1_take_x, "take!", 2, 0, 0,
  1579. (SCM lst, SCM n),
  1580. "Return a list containing the first @var{n} elements of\n"
  1581. "@var{lst}.")
  1582. #define FUNC_NAME s_scm_srfi1_take_x
  1583. {
  1584. long nn;
  1585. SCM pos;
  1586. nn = scm_to_signed_integer (n, 0, LONG_MAX);
  1587. if (nn == 0)
  1588. return SCM_EOL;
  1589. pos = scm_list_tail (lst, scm_from_long (nn - 1));
  1590. /* Must have at least one cell left, mustn't have reached the end of an
  1591. n-1 element list. SCM_VALIDATE_CONS here gives the same error as
  1592. scm_list_tail does on say an n-2 element list, though perhaps a range
  1593. error would make more sense (for both). */
  1594. SCM_VALIDATE_CONS (SCM_ARG1, pos);
  1595. SCM_SETCDR (pos, SCM_EOL);
  1596. return lst;
  1597. }
  1598. #undef FUNC_NAME
  1599. SCM_DEFINE (scm_srfi1_take_right, "take-right", 2, 0, 0,
  1600. (SCM lst, SCM n),
  1601. "Return the a list containing the @var{n} last elements of\n"
  1602. "@var{lst}.")
  1603. #define FUNC_NAME s_scm_srfi1_take_right
  1604. {
  1605. SCM tail = scm_list_tail (lst, n);
  1606. while (scm_is_pair (tail))
  1607. {
  1608. lst = SCM_CDR (lst);
  1609. tail = SCM_CDR (tail);
  1610. }
  1611. SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P(tail), tail, SCM_ARG1, FUNC_NAME, "list");
  1612. return lst;
  1613. }
  1614. #undef FUNC_NAME
  1615. SCM_DEFINE (scm_srfi1_take_while, "take-while", 2, 0, 0,
  1616. (SCM pred, SCM lst),
  1617. "Return a new list which is the longest initial prefix of\n"
  1618. "@var{lst} whose elements all satisfy the predicate @var{pred}.")
  1619. #define FUNC_NAME s_scm_srfi1_take_while
  1620. {
  1621. SCM ret, *p;
  1622. SCM_ASSERT (scm_is_true (scm_procedure_p (pred)), pred, SCM_ARG1, FUNC_NAME);
  1623. ret = SCM_EOL;
  1624. p = &ret;
  1625. for ( ; scm_is_pair (lst); lst = SCM_CDR (lst))
  1626. {
  1627. SCM elem = SCM_CAR (lst);
  1628. if (scm_is_false (scm_call_1 (pred, elem)))
  1629. goto done;
  1630. /* want this elem, tack it onto the end of ret */
  1631. *p = scm_cons (elem, SCM_EOL);
  1632. p = SCM_CDRLOC (*p);
  1633. }
  1634. SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst), lst, SCM_ARG2, FUNC_NAME, "list");
  1635. done:
  1636. return ret;
  1637. }
  1638. #undef FUNC_NAME
  1639. SCM_DEFINE (scm_srfi1_take_while_x, "take-while!", 2, 0, 0,
  1640. (SCM pred, SCM lst),
  1641. "Return the longest initial prefix of @var{lst} whose elements\n"
  1642. "all satisfy the predicate @var{pred}. @var{lst} may be\n"
  1643. "modified to form the return.")
  1644. #define FUNC_NAME s_scm_srfi1_take_while_x
  1645. {
  1646. SCM upto, *p;
  1647. SCM_ASSERT (scm_is_true (scm_procedure_p (pred)), pred, SCM_ARG1, FUNC_NAME);
  1648. p = &lst;
  1649. for (upto = lst; scm_is_pair (upto); upto = SCM_CDR (upto))
  1650. {
  1651. if (scm_is_false (scm_call_1 (pred, SCM_CAR (upto))))
  1652. goto done;
  1653. /* want this element */
  1654. p = SCM_CDRLOC (upto);
  1655. }
  1656. SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (upto), lst, SCM_ARG2, FUNC_NAME, "list");
  1657. done:
  1658. *p = SCM_EOL;
  1659. return lst;
  1660. }
  1661. #undef FUNC_NAME
  1662. SCM_DEFINE (scm_srfi1_tenth, "tenth", 1, 0, 0,
  1663. (SCM lst),
  1664. "Return the tenth element of @var{lst}.")
  1665. #define FUNC_NAME s_scm_srfi1_tenth
  1666. {
  1667. return scm_list_ref (lst, scm_from_int (9));
  1668. }
  1669. #undef FUNC_NAME
  1670. SCM
  1671. scm_srfi1_xcons (SCM d, SCM a)
  1672. {
  1673. CACHE_VAR (xcons, "xcons");
  1674. return scm_call_2 (xcons, d, a);
  1675. }
  1676. void
  1677. scm_init_srfi_1 (void)
  1678. {
  1679. SCM the_root_module = scm_lookup_closure_module (SCM_BOOL_F);
  1680. #ifndef SCM_MAGIC_SNARFER
  1681. #include "srfi/srfi-1.x"
  1682. #endif
  1683. scm_c_extend_primitive_generic
  1684. (SCM_VARIABLE_REF (scm_c_module_lookup (the_root_module, "map")),
  1685. SCM_VARIABLE_REF (scm_c_lookup ("map")));
  1686. scm_c_extend_primitive_generic
  1687. (SCM_VARIABLE_REF (scm_c_module_lookup (the_root_module, "for-each")),
  1688. SCM_VARIABLE_REF (scm_c_lookup ("for-each")));
  1689. }
  1690. /* End of srfi-1.c. */