srfi-1.c 67 KB

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