list.c 24.5 KB
Newer Older
eg's avatar
eg committed
1 2
/*
 *
3
 * l i s t . c                  -- Lists procedures
eg's avatar
eg committed
4
 *
5
 * Copyright © 1993-2018 Erick Gallesio - I3S-CNRS/ESSI <eg@unice.fr>
eg's avatar
eg committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
 * USA.
 *
 *           Author: Erick Gallesio [eg@kaolin.unice.fr]
 *    Creation date: ??-Oct-1993 21:37
25
 * Last file update: 27-Nov-2018 17:22 (eg)
eg's avatar
eg committed
26 27 28 29 30 31
 */

#include "stklos.h"

/*===========================================================================*\
 *
32
 *                              Utilities
eg's avatar
eg committed
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
 *
\*===========================================================================*/

static void error_wrong_type(SCM x)
{
  STk_error("wrong type of argument ~S", x);
}

static void error_const_cell(SCM x)
{
  STk_error("changing the constant ~s is not allowed", x);
}

static void error_bad_list(SCM x)
{
48
  STk_error("bad list ~W", x);
eg's avatar
eg committed
49 50 51 52 53 54 55 56 57
}

static void error_bad_proc(SCM x)
{
  STk_error("bad procedure ~S", x);
}

static void error_circular_list(SCM x)
{
58
  STk_error("list ~W is circular", x);
eg's avatar
eg committed
59 60
}

61 62 63 64 65 66 67 68 69
static void error_too_short(SCM x)
{
  STk_error("list ~W too short", x);
}

static void error_not_exact_positive(SCM x)
{
  STk_error("index ~W is not an exact positive integer", x);
}
eg's avatar
eg committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86

int STk_int_length(SCM l)
{
  register SCM start = l;
  register int len   = 0;

  for ( ; ; ) {
    if (NULLP(l)) return len;
    if ((l == start && len) || !CONSP(l)) return -1;
    l = CDR(l);
    len += 1;
  }
}

SCM STk_argv2list(int argc, SCM *argv)
{
  SCM res = STk_nil;
87

eg's avatar
eg committed
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
  while (argc--) {
    res = STk_cons(argv[-argc], res);
  }
  return res;
}



DEFINE_PRIMITIVE("pair?", pairp, subr1, (SCM x))
/*
<doc pair?
 * (pair? obj)
 *
 * |Pair?| returns |#t| if |obj| is a pair, and otherwise returns |#f|.
doc>
 */
{
  return CONSP(x) ? STk_true : STk_false;
}



DEFINE_PRIMITIVE("cons", cons, subr2, (SCM x, SCM y))
/*
<doc cons
 * (cons obj1 obj2)
 *
 * Returns a newly allocated pair whose car is obj1 and whose cdr is obj2.
 * The pair is guaranteed to be different (in the sense of eqv?) from every
 * existing object.
 * @lisp
 *     (cons 'a '())           =>  (a)
 *     (cons '(a) '(b c d))    =>  ((a) b c d)
 *     (cons "a" '(b c))       =>  ("a" b c)
 *     (cons 'a 3)             =>  (a . 3)
 *     (cons '(a b) 'c)        =>  ((a b) . c)
 * @end lisp
doc>
 */
{
  SCM z;

  NEWCELL(z, cons);
  CAR(z) = x;
  CDR(z) = y;
  return z;
}


DEFINE_PRIMITIVE("car", car, subr1, (SCM x))
/*
<doc car
 * (car pair)
 *
 * Returns the contents of the car field of pair.
 * Note that it is an error to take the |car| of the empty list.
 * @lisp
 *     (car '(a b c))          =>  a
 *     (car '((a) b c d))      =>  (a)
 *     (car '(1 . 2))          =>  1
 *     (car '())               =>  error
 * @end lisp
doc>
 */
{
  if (CONSP(x)) return CAR(x);
  error_wrong_type(x);
  return STk_void; /* never reached */
}


DEFINE_PRIMITIVE("cdr", cdr, subr1, (SCM x))
/*
<doc cdr
 * (cdr pair)
 *
 * Returns the contents of the cdr field of pair.
 * Note that it is an error to take the |cdr| of the empty list.
 * @lisp
 *     (cdr '((a) b c d))      =>  (b c d)
 *     (cdr '(1 . 2))          =>  2
 *     (cdr '())               =>  error
 * @end lisp
doc>
 */
{
  if (CONSP(x)) return CDR(x);
  error_wrong_type(x);
  return STk_void; /* never reached */
}


DEFINE_PRIMITIVE("set-car!", setcar, subr2, (SCM cell, SCM value))
/*
<doc set-car!
 * (set-car! pair obj)
 *
 * Stores |obj| in the car field of |pair|.
 * The value returned by |set-car!| is ,(emph "void").
 * @lisp
 *    (define (f) (list 'not-a-constant-list))
 *    (define (g) '(constant-list))
 *    (set-car! (f) 3)
 *    (set-car! (g) 3)             =>  error
 * @end lisp
doc>
 */
{
196
  if (!CONSP(cell))                  error_wrong_type(cell);
eg's avatar
eg committed
197 198 199 200 201 202 203 204 205 206 207 208 209 210
  if (BOXED_INFO(cell) & CONS_CONST) error_const_cell(cell);

  CAR(cell) = value;
  return STk_void;
}


DEFINE_PRIMITIVE("set-cdr!", setcdr, subr2, (SCM cell, SCM value))
/*
<doc set-cdr!
 * (set-cdr! pair obj)
 *
 * Stores |obj| in the cdr field of |pair|.
 * The value returned by |set-cdr!| is ,(emph "void").
211
 *
eg's avatar
eg committed
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
doc>
 */
{
  if (!CONSP(cell)) error_wrong_type(cell);
  if (BOXED_INFO(cell) & CONS_CONST) error_const_cell(cell);

  CDR(cell) = value;
  return STk_void;
}


DEFINE_PRIMITIVE("null?", nullp, subr1, (SCM x))
/*
<doc null?
 * (null? obj)
 *
 * Returns |#t| if |obj| is the empty list, otherwise returns |#f|.
doc>
 */
{
  return MAKE_BOOLEAN(x == STk_nil);
}


DEFINE_PRIMITIVE("list?", listp, subr1, (SCM x))
/*
<doc list?
 * (list? obj)
 *
 * Returns |#t| if |obj| is a list, otherwise returns |#f|. By definition,
 * all lists have finite length and are terminated by the empty list.
 * @lisp
 *    (list? '(a b c))     =>  #t
 *    (list? '())          =>  #t
 *    (list? '(a . b))     =>  #f
 *    (let ((x (list 'a)))
 *      (set-cdr! x x)
 *      (list? x))         =>  #f
 * @end lisp
doc>
 */
{
  return MAKE_BOOLEAN(STk_int_length(x) >= 0);
}

DEFINE_PRIMITIVE("list", list, vsubr, (int argc, SCM * argv))
/*
<doc list
 * (list obj ...)
 *
 * Returns a newly allocated list of its arguments.
 * @lisp
 *    (list 'a (+ 3 4) 'c)            =>  (a 7 c)
 *    (list)                          =>  ()
 * @end lisp
doc>
 */
{
  register SCM *tmp, l = STk_nil;

  for (tmp = argv-argc+1; tmp <= argv; tmp++)
    l = STk_cons(*tmp, l);

  return l;
}

DEFINE_PRIMITIVE("length", list_length, subr1, (SCM l))
/*
<doc length
 * (length list)
 *
 * Returns the length of |list|.
 *
 * @lisp
 *    (length '(a b c))               =>  3
 *    (length '(a (b) (c d e)))       =>  3
 *    (length '())                    =>  0
 * @end lisp
doc>
 */
{
  int len = STk_int_length(l);

  if (len >= 0) return MAKE_INT(len);
296
  STk_error("length of ~W is not calculable", l);
eg's avatar
eg committed
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
  return STk_void; /* never reached */
}


/*
<doc append
 * (append list ...)
 *
 * Returns a list consisting of the elements of the first list
 * followed by the elements of the other lists.
 *
 * @lisp
 *    (append '(x) '(y))              =>  (x y)
 *    (append '(a) '(b c d))          =>  (a b c d)
 *    (append '(a (b)) '((c)))        =>  (a (b) (c))
 * @end lisp
 *
 * The resulting list is always newly allocated, except that it shares
 * structure with the last list argument. The last argument may actually
 * be any object; an improper list results if the last argument is not a
 * proper list.
 *
 * @lisp
 *    (append '(a b) '(c . d))        =>  (a b c . d)
 *    (append '() 'a)                 =>  a
 * @end lisp
doc>
 */
SCM STk_append2(SCM l1, SCM l2)
{
  register SCM prev, tmp, l;
  SCM res;

  if (NULLP(l1)) return l2;
  if (!CONSP(l1)) goto Error;

  prev = res = STk_nil;
  for (l = l1; ; l = CDR(l)) {
    if (NULLP(l)) break;
    if (!CONSP(l)) goto Error;
    tmp = STk_cons(CAR(l), STk_nil);

    if (res == STk_nil) {
      prev = res = tmp;
    } else {
      CDR(prev) = tmp;
      prev = tmp;
    }
  }
  CDR(prev) = l2;
  return res;
Error:
  STk_error("argument ~S is not a list", l1);
  return STk_void; /* never reached */
}


DEFINE_PRIMITIVE("append", append, vsubr, (int argc, SCM* argv))
{
  switch (argc) {
    case 0:  return STk_nil;
    case 1:  return *argv;
    case 2:  return STk_append2(argv[0], argv[-1]);
    default: return STk_append2(argv[0], STk_append(argc-1, argv-1));
  }
}



/*
<doc reverse
 * (reverse list)
 *
 * Returns a newly allocated list consisting of the elements of |list| in
 * reverse order.
 *
 * @lisp
 *    (reverse '(a b c))              =>  (c b a)
 *    (reverse '(a (b c) d (e (f))))  =>  ((e (f)) d (b c) a)
 * @end lisp
doc>
 */
DEFINE_PRIMITIVE("reverse", reverse, subr1, (SCM l))
{
  SCM p, n = STk_nil;

  for(p=l; !NULLP(p); p=CDR(p)) {
384
    if (!CONSP(p)) error_bad_list(l);
eg's avatar
eg committed
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
    n = STk_cons(CAR(p), n);
  }
  return n;
}

/*
<doc list-tail
 * (list-tail list k)
 *
 * Returns the sublist of |list| obtained by omitting the first |k| elements.
 * It is an error if list has fewer than |k| elements. List-tail could
 * be defined by
 * @lisp
 *    (define list-tail
 *       (lambda (x k)
 *          (if (zero? k)
 *             x
 *             (list-tail (cdr x) (- k 1)))))
 * @end lisp
doc>
 */
DEFINE_PRIMITIVE("list-tail", list_tail, subr2, (SCM list, SCM k))
{
  register long x;
  SCM l;

411
  if (!CONSP(list) && !NULLP(list)) error_bad_list(list);
eg's avatar
eg committed
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446

  x = STk_integer_value(k);
  if (x >= 0) {
    for (l=list; x > 0; x--) {
      if (NULLP(l) || !CONSP(l)) STk_error("list ~S too short", list);
      l = CDR(l);
    }
    return l;
  }

  STk_error("index ~S is not an exact positive integer", k);
  return STk_void; /* never reached */
}


/*
<doc list-ref
 * (list-ref list k)
 *
 * Returns the |k|th element of |list|. (This is the same as the car
 * of |(list-tail list k)|.) It is an error if list has fewer than |k|
 * elements.
 *
 * @lisp
 *    (list-ref '(a b c d) 2)                 =>  c
 *    (list-ref '(a b c d)
 *              (inexact->exact (round 1.8))) =>  c
 * @end lisp
doc>
 */
DEFINE_PRIMITIVE("list-ref", list_ref, subr2, (SCM list, SCM k))
{
  register long x;
  SCM l = list;

447
  if (!CONSP(list)) error_bad_list(list);
eg's avatar
eg committed
448 449 450 451 452 453 454 455 456

  x = STk_integer_value(k);
  if (x >= 0) {
    for ( ; x > 0; x--) {
      if (NULLP(l) || !CONSP(l)) goto Error;
      l = CDR(l);
    }
    if (CONSP(l)) return CAR(l);
  Error:
457
    error_too_short(list);
eg's avatar
eg committed
458
  }
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
  error_not_exact_positive(k);
  return STk_void; /* never reached */
}

/*
<doc R7RS list-set!
 * (list-set! list k obj)
 *
 * The |list-set!| procedure stores |obj| in element |k| of |list|.
 * It is an error if |k| is not a valid index of |list|.
 * @lisp
 * (let ((ls (list 'one 'two 'five!)))
 *    (list-set! ls 2 'three)
 *    ls)                              => (one two three)
 * (list-set! ’(0 1 2) 1 "oops")       => error (constant list)
 * @end lisp
doc>
*/
DEFINE_PRIMITIVE("list-set!", list_set, subr3, (SCM list, SCM k, SCM obj))
{
  register long x;
  SCM l = list;

  if (!CONSP(list)) error_bad_list(list);

  x = STk_integer_value(k);
  if (x >= 0) {
    for ( ; x > 0; x--) {
      if (NULLP(l) || !CONSP(l)) goto Error;
      l = CDR(l);
    }
    if (CONSP(l)) {
      if (BOXED_INFO(l) & CONS_CONST) error_const_cell(list);
      CAR(l) = obj;
      return STk_void;
    }
  Error:
    error_too_short(list);
  }
  error_not_exact_positive(k);
eg's avatar
eg committed
499 500 501 502 503
  return STk_void; /* never reached */
}


/*
504
<doc R57RS memq memv member
eg's avatar
eg committed
505 506 507
 * (memq obj list)
 * (memv obj list)
 * (member obj list)
508
 * (member obj list compare)
eg's avatar
eg committed
509 510 511 512 513 514
 *
 * These procedures return the first sublist of list whose car is |obj|,
 * where the sublists of list are the non-empty lists returned by
 * |(list-tail list k)| for |k| less than the length of list.
 * If |obj| does not occur in |list|, then |#f| (not the empty list) is
 * returned. |Memq| uses |eq?| to compare obj with the elements of list,
515 516
 * while |memv| uses |eqv?| and |member| uses |compare|, if given, and 
 * |equal?| otherwise.
eg's avatar
eg committed
517 518 519 520 521 522 523 524
 *
 * @lisp
 *    (memq 'a '(a b c))              =>  (a b c)
 *    (memq 'b '(a b c))              =>  (b c)
 *    (memq 'a '(b c d))              =>  #f
 *    (memq (list 'a) '(b (a) c))     =>  #f
 *    (member (list 'a)
 *            '(b (a) c))             =>  ((a) c)
525 526 527
 *    (member "B"
 *            ’("a" "b" "c")
 *            string-ci=?)            => ("b" "c")
eg's avatar
eg committed
528 529
 *    (memv 101 '(100 101 102))       =>  (101 102)
 * @end lisp
530
 *
531
 * ,(bold "Note:") As in R7RS, the |member| function accepts also a
532
 * comparison function.
eg's avatar
eg committed
533 534 535
doc>
 */

536 537 538 539
#define PTR_EQ(x, y)            ((x) == (y))
#define PTR_EQV(x, y)           (STk_eqv((x), (y)) != STk_false)
#define PTR_EQUAL(x, y)         (STk_equal((x), (y)) != STk_false)
#define PTR_CMPGEN(x, y)        (STk_C_apply(cmp, 2, (x), (y)) != STk_false)
eg's avatar
eg committed
540 541


542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
#define LMEMBER(compare)                                        \
{                                                               \
  register SCM ptr;                                             \
                                                                \
  if (!CONSP(list) && !NULLP(list)) error_bad_list(list);       \
                                                                \
  for (ptr=list; !NULLP(ptr); ) {                               \
    if (CONSP(ptr)) {                                           \
      if (compare(CAR(ptr), obj)) return ptr;                   \
    }                                                           \
    else                                                        \
      break; /* end of a dotted list */                         \
    if ((ptr=CDR(ptr)) == list) error_circular_list(ptr);       \
  }                                                             \
  return STk_false;                                             \
eg's avatar
eg committed
557 558 559 560
}


DEFINE_PRIMITIVE("memq", memq, subr2, (SCM obj, SCM list))
561
     LMEMBER(PTR_EQ)
eg's avatar
eg committed
562 563

DEFINE_PRIMITIVE("memv", memv, subr2, (SCM obj, SCM list))
564 565 566 567 568 569 570
     LMEMBER(PTR_EQV)

DEFINE_PRIMITIVE("member", member, subr23, (SCM obj, SCM list, SCM cmp))
{
  if (cmp) {
    if (STk_procedurep(cmp) != STk_true)
      STk_error("bad comparison function ~S", cmp);
eg's avatar
eg committed
571

572 573 574 575 576
    LMEMBER(PTR_CMPGEN);
  } else {
    LMEMBER(PTR_EQUAL);
  }
}
eg's avatar
eg committed
577 578 579


/*
580
<doc R57RS assq assv assoc
eg's avatar
eg committed
581 582 583
 * (assq obj alist)
 * (assv obj alist)
 * (assoc obj alist)
584
 * (assoc obj alist compare)
eg's avatar
eg committed
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
 *
 * |Alist| (for "association list") must be a list of pairs. These procedures
 * find the first pair in |alist| whose car field is |obj|, and returns that
 * pair. If no pair in |alist| has |obj| as its car, then |#f| (not the empty
 * list) is returned. |Assq| uses |eq?| to compare |obj| with the car fields
 * of the pairs in |alist|, while |assv| uses |eqv?| and |assoc| uses |equal?|.
 *
 * @lisp
 *    (define e '((a 1) (b 2) (c 3)))
 *    (assq 'a e)                =>  (a 1)
 *    (assq 'b e)                =>  (b 2)
 *    (assq 'd e)                =>  #f
 *    (assq (list 'a) '(((a)) ((b)) ((c))))
 *                               =>  #f
 *    (assoc (list 'a) '(((a)) ((b)) ((c))))
600 601 602
 *                               => ((a))
 *    (assoc 2.0 '((1 1) (2 4) (3 9)) =)
 *                               => (2 4)
eg's avatar
eg committed
603 604 605 606 607 608 609 610
 *    (assv 5 '((2 3) (5 7) (11 13)))
 *                               =>  (5 7)
 * @end lisp
 *
 * ,(bold "Rationale:") Although they are ordinarily used as predicates,
 * |memq|, |memv|, |member|, |assq|, |assv|, and |assoc| do not have question
 * marks in their names because they return useful values rather than just
 * |#t| or #|f|.
611 612 613
 *
 * ,(bold "Note:") As in R7RS, the |assoc| function accepts also a
 * comparison function.
eg's avatar
eg committed
614 615 616
doc>
 */

617 618 619 620 621 622 623 624 625 626 627 628
#define LASSOC(compare)                                         \
{                                                               \
  register SCM l,tmp;                                           \
                                                                \
  for(l=alist; CONSP(l); ) {                                    \
    tmp = CAR(l);                                               \
    if (CONSP(tmp) && compare(CAR(tmp), obj)) return tmp;       \
    if ((l=CDR(l)) == alist) break;                             \
  }                                                             \
  if (NULLP(l)) return(STk_false);                              \
  STk_error("improper list ~W", alist);                         \
  return STk_void; /* never reached */                          \
eg's avatar
eg committed
629 630 631
}

DEFINE_PRIMITIVE("assq", assq, subr2, (SCM obj, SCM alist))
632
     LASSOC(PTR_EQ)
eg's avatar
eg committed
633 634

DEFINE_PRIMITIVE("assv", assv, subr2, (SCM obj, SCM alist))
635
     LASSOC(PTR_EQV)
eg's avatar
eg committed
636

637 638 639 640 641 642 643 644 645 646 647
DEFINE_PRIMITIVE("assoc", assoc, subr23, (SCM obj, SCM alist, SCM cmp))
{
  if (cmp) {
    if (STk_procedurep(cmp) != STk_true)
      STk_error("bad comparison function ~S", cmp);

    LASSOC(PTR_CMPGEN);
 } else {
   LASSOC(PTR_EQUAL);
 }
}
eg's avatar
eg committed
648 649


650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665
/*
<doc R7RS list-copy
 * (list-copy obj)
 *
 * |list-copy| recursively copies trees of pairs. If |obj| is
 * not a pair, it is returned; otherwise the result is a new pair whose
 * |car| and |cdr| are obtained by calling |list-copy| on
 * the |car| and |cdr| of |obj|, respectively.
doc>
 */
DEFINE_PRIMITIVE("list-copy", list_copy, subr1, (SCM l))
{
  return CONSP(l) ? STk_cons(STk_list_copy(CAR(l)), STk_list_copy(CDR(l))): l;
}


eg's avatar
eg committed
666 667 668 669 670 671 672 673 674 675
/***
 *
 * Non standard functions
 *
 ***/


/*
<doc EXT pair-mutable?
 * (pair-mutable? obj)
676 677
 *
 * Returns |#t| if |obj| is a mutable pair, otherwise returns |#f|.
eg's avatar
eg committed
678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734
 * @lisp
 * (pair-mutable? '(1 . 2))    => #f
 * (pair-mutable? (cons 1 2))  => #t
 * (pair-mutable? 12)          => #f
 * @end lisp
doc>
*/
DEFINE_PRIMITIVE("pair-mutable?", pair_mutable, subr1, (SCM obj))
{
  return MAKE_BOOLEAN(CONSP(obj) && !(BOXED_INFO(obj) & CONS_CONST));
}


/*
<doc EXT list*
 * (list* obj ...)
 *
 * |list*| is like |list| except that the last argument to |list*| is
 * used as the ,(emph "cdr") of the last pair constructed.
 * @lisp
 *    (list* 1 2 3)        => (1 2 . 3)
 *    (list* 1 2 3 '(4 5)) => (1 2 3 4 5)
 *    (list*)              => ()
 * @end lisp
doc>
 */
DEFINE_PRIMITIVE("list*", list_star, vsubr, (int argc, SCM *argv))
{
  register SCM *tmp, l;

  if (argc == 0) return STk_nil;

  tmp = argv-argc+1;
  l   = *tmp;

  for (++tmp; tmp <= argv; tmp++)
    l = STk_cons(*tmp, l);

  return l;
}



/*
<doc EXT last-pair
 * (last-pair list)
 *
 * Returns the last pair of |list|.
 * @lisp
 * (last-pair '(1 2 3))   => (3)
 * (last-pair '(1 2 . 3)) => (2 . 3)
 * @end lisp
doc>
 */
DEFINE_PRIMITIVE("last-pair", last_pair, subr1, (SCM l))
{
  if (!CONSP(l)) error_wrong_type(l);
Erick's avatar
.  
Erick committed
735 736 737 738

  while (CONSP(CDR(l)))
    l = CDR(l);

eg's avatar
eg committed
739 740 741 742
  return l;
}

/*
743
<doc EXT filter filter!
eg's avatar
eg committed
744 745 746 747 748 749
 * (filter  pred list)
 * (filter! pred list)
 *
 * |Filter| returns all the elements of |list| that satisfy predicate
 * |pred|. The |list| is not disordered: elements that appear in the
 * result list occur in the same order as they occur in the argument
750 751
 * list. |Filter!| does the same job than |filter| by physically
 * modifying its |list| argument
eg's avatar
eg committed
752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770
 * @lisp
 * (filter even? '(0 7 8 8 43 -4)) => (0 8 8 -4)
 * (let* ((l1 (list 0 7 8 8 43 -4))
 *        (l2 (filter! even? l1)))
 *    (list l1 l2))                => ((0 8 8 -4) (0 8 8 -4))
 * @end lisp
 * An error is signaled if |list| is a constant list.
doc>
 */
DEFINE_PRIMITIVE("filter", filter, subr2, (SCM pred, SCM list))
{
  register SCM ptr, l;
  SCM result;

  if (STk_procedurep(pred) != STk_true) error_bad_proc(pred);
  if (!CONSP(list) && !NULLP(list)) error_bad_list(list);

  for (ptr=l=list, result=STk_nil; !NULLP(l); ) {
    if (!CONSP(l)) error_bad_list(list);
771

eg's avatar
eg committed
772 773
    if (STk_C_apply(pred, 1, CAR(l)) != STk_false) {
      if (NULLP(result)) {
774 775
        NEWCELL(result, cons);
        ptr = result;
eg's avatar
eg committed
776 777
      }
      else {
778 779
        NEWCELL(CDR(ptr), cons);
        ptr = CDR(ptr);
eg's avatar
eg committed
780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802
      }
      CAR(ptr) = CAR(l);
      CDR(ptr) = STk_nil;
    }
    if ((l=CDR(l)) == list) error_circular_list(list);
  }
  return result;
}


DEFINE_PRIMITIVE("filter!", dfilter, subr2, (SCM pred, SCM list))
{
  SCM previous, l, start=list;

  if (STk_procedurep(pred) != STk_true) error_bad_proc(pred);
  if (!CONSP(list) && !NULLP(list)) error_bad_list(list);

  for (previous=STk_nil, l=list; !NULLP(l); ) {
    if (!CONSP(l)) error_bad_list(list);
    if (BOXED_INFO(l) & CONS_CONST) error_const_cell(l);

    if (STk_C_apply(pred, 1, CAR(l)) == STk_false) {
      if (previous == STk_nil)
803
        list = CDR(list);
804
      else
805
        CDR(previous) = CDR(l);
eg's avatar
eg committed
806 807 808 809 810 811 812 813 814 815 816 817 818 819
    } else {
      previous = l;
    }
    if ((l=CDR(l)) == start) error_circular_list(list);
  }
  return list;
}

/*
<doc EXT append!
 * (append! list ...)
 *
 * Returns a list consisting of the elements of the first list
 * followed by the elements of the other lists.
820 821
 * Contrarily to |append|, the parameter lists (except the last one) are
 * physically modified: their last pair is changed to the value of the next
eg's avatar
eg committed
822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904
 * list in the |append!| formal parameter list.
 * @lisp
 * (let* ((l1 (list 1 2))
 *        (l2 (list 3))
 *        (l3 (list 4 5))
 *        (l4 (append! l1 l2 l3)))
 *   (list l1 l2 l3))  => ((1 2 3 4 5) (3 4 5) (4 5))
 * @end lisp
 * An error is signaled if one of the given lists is a constant list.
doc>
*/

SCM STk_dappend2(SCM l1, SCM l2)
{
  register SCM tmp;

  if (NULLP(l1)) return l2;

  for (tmp = l1; ; tmp = CDR(tmp)) {
    if (BOXED_INFO(tmp) & CONS_CONST) error_const_cell(tmp);
    if (NULLP(CDR(tmp))) {
      CDR(tmp) = l2;
      return l1;
    }
  }
  STk_error("bad pair ~S", tmp);
  return STk_void; /* never reached */
}


DEFINE_PRIMITIVE("append!", dappend, vsubr, (int argc, SCM* argv))
{
  switch (argc) {
    case 0:  return STk_nil;
    case 1:  return *argv;
    case 2:  return STk_dappend2(argv[0], argv[-1]);
    default: return STk_dappend2(argv[0], STk_dappend(argc-1, argv-1));
  }
}

/*
<doc EXT reverse!
 * (reverse! list)
 *
 * Returns a list consisting of the elements of |list| in reverse order.
 * Contrarily to |reverse|, the returned value is not newly allocated but
 * computed "in place".
 *
 * @lisp
 * (let ((l '(a b c)))
 *   (list (reverse! l) l))        =>  ((c b a) (a))
 * (reverse! '(a constant list))   =>  error
 * @end lisp
doc>
 */
DEFINE_PRIMITIVE("reverse!", dreverse, subr1, (SCM l))
{
  SCM tmp, p, prev;

  for(p=l, prev=STk_nil; !NULLP(p); prev=p, p=tmp) {
    if (!CONSP(p)) STk_error("bad list ~W", l);
    if (BOXED_INFO(p) & CONS_CONST) error_const_cell(p);
    tmp = CDR(p);
    CDR(p) = prev;
  }
  return prev;
}

/*
 *
 * Utility procedure equivalent to (delete! obj list eq?)
 *
 */
SCM STk_dremq(SCM obj, SCM list)
{
  SCM previous, l, start=list;

  for (previous=STk_nil, l=list; !NULLP(l); ) {
    if (!CONSP(l)) error_bad_list(list);
    if (BOXED_INFO(l) & CONS_CONST) error_const_cell(l);

    if (obj == CAR(l)) {
      if (previous == STk_nil)
905
        list = CDR(list);
906
      else
907
        CDR(previous) = CDR(l);
eg's avatar
eg committed
908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923
    } else {
      previous = l;
    }
    if ((l=CDR(l)) == start) error_circular_list(list);
  }
  return list;
}

/*
 *
 * Fast version of assq; for internal use only (alist must be well formed)
 *
 */
SCM STk_int_assq(SCM obj, SCM alist)
{
  register SCM l;
924

eg's avatar
eg committed
925 926 927 928 929 930 931 932 933 934
  for(l=alist; !NULLP(l); l = CDR(l)) {
    if (CAR(CAR(l)) == obj) return CAR(l);
  }
  return STk_false;
}



/* ======================================================================
 *
935
 * Extended pairs
eg's avatar
eg committed
936 937 938 939
 *
 * ======================================================================
 */

940
struct econs_obj {              /* Use a mapping wchich is identical to cons */
eg's avatar
eg committed
941 942 943 944 945 946 947 948
  stk_header header;
  SCM car;
  SCM cdr;
  char *file;
  int line;
  int pos;
};

949 950 951 952
#define ECONS_FILE(p)   (((struct econs_obj *) (p))->file)
#define ECONS_LINE(p)   (((struct econs_obj *) (p))->line)
#define ECONS_POS(p)    (((struct econs_obj *) (p))->pos)
#define ECONSP(obj)     (CONSP(obj) && (BOXED_INFO(obj) & CONS_ECONS))
eg's avatar
eg committed
953 954 955 956 957 958 959


SCM STk_econs(SCM car, SCM cdr, char *file, int line, int pos)
{
  SCM z;

  NEWCELL_WITH_LEN(z, cons, sizeof(struct econs_obj));
960 961
  CAR(z)        = car;
  CDR(z)        = cdr;
eg's avatar
eg committed
962 963 964 965
  ECONS_FILE(z) = file;
  ECONS_LINE(z) = line;
  ECONS_POS(z)  = pos;
  BOXED_INFO(z) |= CONS_ECONS;
966

eg's avatar
eg committed
967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
  return z;
}

DEFINE_PRIMITIVE("%epair?", epairp, subr1, (SCM obj))
{
  return MAKE_BOOLEAN(ECONSP(obj));
}

DEFINE_PRIMITIVE("%epair-file", epair_file, subr1, (SCM obj))
{
  if (ECONSP(obj)) return STk_Cstring2string(ECONS_FILE(obj));
  error_wrong_type(obj);
  return STk_void; /* never reached */
}


DEFINE_PRIMITIVE("%epair-line", epair_line, subr1, (SCM obj))
{
  if (ECONSP(obj)) return MAKE_INT(ECONS_LINE(obj));
  error_wrong_type(obj);
  return STk_void; /* never reached */
}


DEFINE_PRIMITIVE("%epair-position", epair_position, subr1, (SCM obj))
{
  if (ECONSP(obj)) return MAKE_INT(ECONS_POS(obj));
  error_wrong_type(obj);
  return STk_void; /* never reached */
}



int STk_init_list(void)
{
  ADD_PRIMITIVE(pairp);
  ADD_PRIMITIVE(cons);
  ADD_PRIMITIVE(car);
  ADD_PRIMITIVE(cdr);
  ADD_PRIMITIVE(setcar);
  ADD_PRIMITIVE(setcdr);
  ADD_PRIMITIVE(nullp);
  ADD_PRIMITIVE(listp);
  ADD_PRIMITIVE(list);
  ADD_PRIMITIVE(list_length);
  ADD_PRIMITIVE(append);
  ADD_PRIMITIVE(reverse);
  ADD_PRIMITIVE(list_tail);
  ADD_PRIMITIVE(list_ref);
1016
  ADD_PRIMITIVE(list_set);
eg's avatar
eg committed
1017 1018 1019 1020 1021 1022
  ADD_PRIMITIVE(memq);
  ADD_PRIMITIVE(memv);
  ADD_PRIMITIVE(member);
  ADD_PRIMITIVE(assq);
  ADD_PRIMITIVE(assv);
  ADD_PRIMITIVE(assoc);
1023 1024
  ADD_PRIMITIVE(list_copy);
 
eg's avatar
eg committed
1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
  ADD_PRIMITIVE(pair_mutable);
  ADD_PRIMITIVE(list_star);
  ADD_PRIMITIVE(last_pair);
  ADD_PRIMITIVE(filter);
  ADD_PRIMITIVE(dfilter);
  ADD_PRIMITIVE(dappend);
  ADD_PRIMITIVE(dreverse);

  ADD_PRIMITIVE(epairp);
  ADD_PRIMITIVE(epair_file);
1035 1036
  ADD_PRIMITIVE(epair_line);
  ADD_PRIMITIVE(epair_position);
eg's avatar
eg committed
1037 1038 1039

  return TRUE;
}