srfi-1.c 67 KB

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