home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / share / guile / 1.6 / srfi / srfi-1.scm next >
Encoding:
Text File  |  2006-06-19  |  25.6 KB  |  1,065 lines

  1. ;;; srfi-1.scm --- List Library
  2.  
  3. ;;     Copyright (C) 2001, 2002, 2004, 2005 Free Software Foundation, Inc.
  4. ;;
  5. ;; This program is free software; you can redistribute it and/or
  6. ;; modify it under the terms of the GNU General Public License as
  7. ;; published by the Free Software Foundation; either version 2, or
  8. ;; (at your option) any later version.
  9. ;;
  10. ;; This program is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13. ;; General Public License for more details.
  14. ;;
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with this software; see the file COPYING.  If not, write to
  17. ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  18. ;; Boston, MA 02110-1301 USA
  19. ;;
  20. ;; As a special exception, the Free Software Foundation gives permission
  21. ;; for additional uses of the text contained in its release of GUILE.
  22. ;;
  23. ;; The exception is that, if you link the GUILE library with other files
  24. ;; to produce an executable, this does not by itself cause the
  25. ;; resulting executable to be covered by the GNU General Public License.
  26. ;; Your use of that executable is in no way restricted on account of
  27. ;; linking the GUILE library code into it.
  28. ;;
  29. ;; This exception does not however invalidate any other reasons why
  30. ;; the executable file might be covered by the GNU General Public License.
  31. ;;
  32. ;; This exception applies only to the code released by the
  33. ;; Free Software Foundation under the name GUILE.  If you copy
  34. ;; code from other Free Software Foundation releases into a copy of
  35. ;; GUILE, as the General Public License permits, the exception does
  36. ;; not apply to the code that you add in this way.  To avoid misleading
  37. ;; anyone as to the status of such modified files, you must delete
  38. ;; this exception notice from them.
  39. ;;
  40. ;; If you write modifications of your own for GUILE, it is your choice
  41. ;; whether to permit this exception to apply to your modifications.
  42. ;; If you do not wish that, delete this exception notice.
  43.  
  44. ;;; Author: Martin Grabmueller <mgrabmue@cs.tu-berlin.de>
  45. ;;; Date: 2001-06-06
  46.  
  47. ;;; Commentary:
  48.  
  49. ;; This is an implementation of SRFI-1 (List Library).
  50. ;;
  51. ;; All procedures defined in SRFI-1, which are not already defined in
  52. ;; the Guile core library, are exported.  The procedures in this
  53. ;; implementation work, but they have not been tuned for speed or
  54. ;; memory usage.
  55. ;;
  56. ;; This module is fully documented in the Guile Reference Manual.
  57.  
  58. ;;; Code:
  59.  
  60. (define-module (srfi srfi-1)
  61.   :use-module (ice-9 session)
  62.   :use-module (ice-9 receive))
  63.  
  64. (begin-deprecated
  65.  ;; Prevent `export' from re-exporting core bindings.  This behaviour
  66.  ;; of `export' is deprecated and will disappear in one of the next
  67.  ;; releases.
  68.  (define iota #f)
  69.  (define list-copy #f)
  70.  (define map #f)
  71.  (define map-in-order #f)
  72.  (define for-each #f)
  73.  (define list-index #f)
  74.  (define member #f)
  75.  (define delete #f)
  76.  (define delete! #f)
  77.  (define assoc #f))
  78.  
  79. (export
  80. ;;; Constructors
  81.  ;; cons                <= in the core
  82.  ;; list                <= in the core
  83.  xcons
  84.  ;; cons*                <= in the core
  85.  ;; make-list                <= in the core
  86.  list-tabulate
  87.  list-copy                ; Extended.
  88.  circular-list
  89.  iota                    ; Extended.
  90.  
  91. ;;; Predicates
  92.  proper-list?
  93.  circular-list?
  94.  dotted-list?
  95.  ;; pair?                <= in the core
  96.  ;; null?                <= in the core
  97.  null-list?
  98.  not-pair?
  99.  list=
  100.  
  101. ;;; Selectors
  102.  ;; car                    <= in the core
  103.  ;; cdr                    <= in the core
  104.  ;; caar                <= in the core
  105.  ;; cadr                <= in the core
  106.  ;; cdar                <= in the core
  107.  ;; cddr                <= in the core
  108.  ;; caaar                <= in the core
  109.  ;; caadr                <= in the core
  110.  ;; cadar                <= in the core
  111.  ;; caddr                <= in the core
  112.  ;; cdaar                <= in the core
  113.  ;; cdadr                <= in the core
  114.  ;; cddar                <= in the core
  115.  ;; cdddr                <= in the core
  116.  ;; caaaar                <= in the core
  117.  ;; caaadr                <= in the core
  118.  ;; caadar                <= in the core
  119.  ;; caaddr                <= in the core
  120.  ;; cadaar                <= in the core
  121.  ;; cadadr                <= in the core
  122.  ;; caddar                <= in the core
  123.  ;; cadddr                <= in the core
  124.  ;; cdaaar                <= in the core
  125.  ;; cdaadr                <= in the core
  126.  ;; cdadar                <= in the core
  127.  ;; cdaddr                <= in the core
  128.  ;; cddaar                <= in the core
  129.  ;; cddadr                <= in the core
  130.  ;; cdddar                <= in the core
  131.  ;; cddddr                <= in the core
  132.  ;; list-ref                <= in the core
  133.  first
  134.  second
  135.  third
  136.  fourth
  137.  fifth
  138.  sixth
  139.  seventh
  140.  eighth
  141.  ninth
  142.  tenth
  143.  car+cdr
  144.  take
  145.  drop
  146.  take-right
  147.  drop-right
  148.  take!
  149.  drop-right!
  150.  split-at
  151.  split-at!
  152.  last
  153.  ;; last-pair                <= in the core
  154.  
  155. ;;; Miscelleneous: length, append, concatenate, reverse, zip & count
  156.  ;; length                <= in the core
  157.  length+
  158.  ;; append                <= in the core
  159.  ;; append!                <= in the core
  160.  concatenate
  161.  concatenate!
  162.  ;; reverse                <= in the core
  163.  ;; reverse!                <= in the core
  164.  append-reverse
  165.  append-reverse!
  166.  zip
  167.  unzip1
  168.  unzip2
  169.  unzip3
  170.  unzip4
  171.  unzip5
  172.  count
  173.  
  174. ;;; Fold, unfold & map
  175.  fold
  176.  fold-right
  177.  pair-fold
  178.  pair-fold-right
  179.  reduce
  180.  reduce-right
  181.  unfold
  182.  unfold-right
  183.  map                    ; Extended.
  184.  for-each                ; Extended.
  185.  append-map
  186.  append-map!
  187.  map!
  188.  map-in-order                ; Extended.
  189.  pair-for-each
  190.  filter-map
  191.  
  192. ;;; Filtering & partitioning
  193.  filter
  194.  partition
  195.  remove
  196.  filter!
  197.  partition!
  198.  remove!
  199.  
  200. ;;; Searching
  201.  find
  202.  find-tail
  203.  take-while
  204.  take-while!
  205.  drop-while
  206.  span
  207.  span!
  208.  break
  209.  break!
  210.  any
  211.  every
  212.  list-index                ; Extended.
  213.  member                    ; Extended.
  214.  ;; memq                <= in the core
  215.  ;; memv                <= in the core
  216.  
  217. ;;; Deletion
  218.  delete                    ; Extended.
  219.  delete!                ; Extended.
  220.  delete-duplicates
  221.  delete-duplicates!
  222.  
  223. ;;; Association lists
  224.  assoc                    ; Extended.
  225.  ;; assq                <= in the core
  226.  ;; assv                <= in the core
  227.  alist-cons
  228.  alist-copy
  229.  alist-delete
  230.  alist-delete!
  231.  
  232. ;;; Set operations on lists
  233.  lset<=
  234.  lset=
  235.  lset-adjoin
  236.  lset-union
  237.  lset-intersection
  238.  lset-difference
  239.  lset-xor
  240.  lset-diff+intersection
  241.  lset-union!
  242.  lset-intersection!
  243.  lset-difference!
  244.  lset-xor!
  245.  lset-diff+intersection!
  246.  
  247. ;;; Primitive side-effects
  248.  ;; set-car!                <= in the core
  249.  ;; set-cdr!                <= in the core
  250.  )
  251.  
  252. (cond-expand-provide (current-module) '(srfi-1))
  253.  
  254. ;;; Constructors
  255.  
  256. (define (xcons d a)
  257.   (cons a d))
  258.  
  259. ;; internal helper, similar to (scsh utilities) check-arg.
  260. (define (check-arg-type pred arg caller)
  261.   (if (pred arg)
  262.       arg
  263.       (scm-error 'wrong-type-arg caller
  264.          "Wrong type argument: ~S" (list arg) '())))
  265.  
  266. ;; the srfi spec doesn't seem to forbid inexact integers.
  267. (define (non-negative-integer? x) (and (integer? x) (>= x 0)))
  268.  
  269. (define (list-tabulate n init-proc)
  270.   (check-arg-type non-negative-integer? n "list-tabulate")
  271.   (let lp ((n n) (acc '()))
  272.     (if (<= n 0)
  273.       acc
  274.       (lp (- n 1) (cons (init-proc (- n 1)) acc)))))
  275.  
  276. (define (list-copy lst)
  277.   (let ((ret (cons #f lst)))
  278.     (do ((lst lst (cdr lst))
  279.      (end ret (cdr end)))
  280.     ((not (pair? lst))
  281.      (cdr ret))
  282.       (set-cdr! end (cons (car lst) (cdr lst))))))
  283.  
  284. (define (circular-list elt1 . rest)
  285.   (let ((start (cons elt1 '())))
  286.     (let lp ((r rest) (p start))
  287.       (if (null? r)
  288.     (begin
  289.       (set-cdr! p start)
  290.       start)
  291.     (begin
  292.       (set-cdr! p (cons (car r) '()))
  293.       (lp (cdr r) (cdr p)))))))
  294.  
  295. (define (iota count . rest)
  296.   (check-arg-type non-negative-integer? count "iota")
  297.   (let ((start (if (pair? rest) (car rest) 0))
  298.     (step (if (and (pair? rest) (pair? (cdr rest))) (cadr rest) 1)))
  299.     (let lp ((n 0) (acc '()))
  300.       (if (= n count)
  301.     (reverse! acc)
  302.     (lp (+ n 1) (cons (+ start (* n step)) acc))))))
  303.  
  304. ;;; Predicates
  305.  
  306. (define (proper-list? x)
  307.   (list? x))
  308.  
  309. (define (circular-list? x)
  310.   (if (not-pair? x)
  311.     #f
  312.     (let lp ((hare (cdr x)) (tortoise x))
  313.       (if (not-pair? hare)
  314.     #f
  315.     (let ((hare (cdr hare)))
  316.       (if (not-pair? hare)
  317.         #f
  318.         (if (eq? hare tortoise)
  319.           #t
  320.           (lp (cdr hare) (cdr tortoise)))))))))
  321.  
  322. (define (dotted-list? x)
  323.   (cond
  324.     ((null? x) #f)
  325.     ((not-pair? x) #t)
  326.     (else
  327.      (let lp ((hare (cdr x)) (tortoise x))
  328.        (cond
  329.      ((null? hare) #f)
  330.      ((not-pair? hare) #t)
  331.      (else
  332.       (let ((hare (cdr hare)))
  333.         (cond
  334.           ((null? hare) #f)
  335.           ((not-pair? hare) #t)
  336.           ((eq? hare tortoise) #f)
  337.           (else
  338.            (lp (cdr hare) (cdr tortoise)))))))))))
  339.  
  340. (define (null-list? x)
  341.   (cond
  342.     ((proper-list? x)
  343.      (null? x))
  344.     ((circular-list? x)
  345.      #f)
  346.     (else
  347.      (error "not a proper list in null-list?"))))
  348.  
  349. (define (not-pair? x)
  350.   (not (pair? x)))
  351.  
  352. (define (list= elt= . rest)
  353.   (define (lists-equal a b)
  354.     (let lp ((a a) (b b))
  355.       (cond ((null? a)
  356.          (null? b))
  357.         ((null? b)
  358.          #f)
  359.         (else
  360.          (and (elt= (car a) (car b))
  361.           (lp (cdr a) (cdr b)))))))
  362.   (or (null? rest)
  363.       (let lp ((lists rest))
  364.     (or (null? (cdr lists))
  365.         (and (lists-equal (car lists) (cadr lists))
  366.          (lp (cdr lists)))))))
  367.  
  368. ;;; Selectors
  369.  
  370. (define first car)
  371. (define second cadr)
  372. (define third caddr)
  373. (define fourth cadddr)
  374. (define (fifth x) (car (cddddr x)))
  375. (define (sixth x) (cadr (cddddr x)))
  376. (define (seventh x) (caddr (cddddr x)))
  377. (define (eighth x) (cadddr (cddddr x)))
  378. (define (ninth x) (car (cddddr (cddddr x))))
  379. (define (tenth x) (cadr (cddddr (cddddr x))))
  380.  
  381. (define (car+cdr x) (values (car x) (cdr x)))
  382.  
  383. (define (take x i)
  384.   (let lp ((n i) (l x) (acc '()))
  385.     (if (<= n 0)
  386.       (reverse! acc)
  387.       (lp (- n 1) (cdr l) (cons (car l) acc)))))
  388. (define (drop x i)
  389.   (let lp ((n i) (l x))
  390.     (if (<= n 0)
  391.       l
  392.       (lp (- n 1) (cdr l)))))
  393. (define (take-right flist i)
  394.   (let lp ((n i) (l flist))
  395.     (if (<= n 0)
  396.       (let lp0 ((s flist) (l l))
  397.     (if (null? l)
  398.       s
  399.       (lp0 (cdr s) (cdr l))))
  400.       (lp (- n 1) (cdr l)))))
  401.  
  402. (define (drop-right flist i)
  403.   (let lp ((n i) (l flist))
  404.     (if (<= n 0)
  405.       (let lp0 ((s flist) (l l) (acc '()))
  406.     (if (null? l)
  407.       (reverse! acc)
  408.       (lp0 (cdr s) (cdr l) (cons (car s) acc))))
  409.       (lp (- n 1) (cdr l)))))
  410.  
  411. (define (take! x i)
  412.   (if (<= i 0)
  413.     '()
  414.     (let lp ((n (- i 1)) (l x))
  415.       (if (<= n 0)
  416.     (begin
  417.       (set-cdr! l '())
  418.       x)
  419.     (lp (- n 1) (cdr l))))))
  420.  
  421. (define (drop-right! flist i)
  422.   (if (<= i 0)
  423.     flist
  424.     (let lp ((n (+ i 1)) (l flist))
  425.       (if (<= n 0)
  426.     (let lp0 ((s flist) (l l))
  427.       (if (null? l)
  428.         (begin
  429.           (set-cdr! s '())
  430.           flist)
  431.         (lp0 (cdr s) (cdr l))))
  432.     (if (null? l)
  433.       '()
  434.       (lp (- n 1) (cdr l)))))))
  435.  
  436. (define (split-at x i)
  437.   (let lp ((l x) (n i) (acc '()))
  438.     (if (<= n 0)
  439.       (values (reverse! acc) l)
  440.       (lp (cdr l) (- n 1) (cons (car l) acc)))))
  441.  
  442. (define (split-at! x i)
  443.   (if (<= i 0)
  444.     (values '() x)
  445.     (let lp ((l x) (n (- i 1)))
  446.       (if (<= n 0)
  447.     (let ((tmp (cdr l)))
  448.       (set-cdr! l '())
  449.       (values x tmp))
  450.     (lp (cdr l) (- n 1))))))
  451.  
  452. (define (last pair)
  453.   (car (last-pair pair)))
  454.  
  455. ;;; Miscelleneous: length, append, concatenate, reverse, zip & count
  456.  
  457. (define (length+ clist)
  458.   (if (null? clist)
  459.     0
  460.     (let lp ((hare (cdr clist)) (tortoise clist) (l 1))
  461.       (if (null? hare)
  462.     l
  463.     (let ((hare (cdr hare)))
  464.       (if (null? hare)
  465.         (+ l 1)
  466.         (if (eq? hare tortoise)
  467.           #f
  468.           (lp (cdr hare) (cdr tortoise) (+ l 2)))))))))
  469.  
  470. (define (concatenate l-o-l)
  471.   (let lp ((l l-o-l) (acc '()))
  472.     (if (null? l)
  473.       (reverse! acc)
  474.       (let lp0 ((ll (car l)) (acc acc))
  475.     (if (null? ll)
  476.       (lp (cdr l) acc)
  477.       (lp0 (cdr ll) (cons (car ll) acc)))))))
  478.  
  479. (define (concatenate! l-o-l)
  480.   (let lp0 ((l-o-l l-o-l))
  481.     (cond
  482.       ((null? l-o-l)
  483.        '())
  484.       ((null? (car l-o-l))
  485.        (lp0 (cdr l-o-l)))
  486.       (else
  487.        (let ((result (car l-o-l)) (tail (last-pair (car l-o-l))))
  488.      (let lp ((l (cdr l-o-l)) (ntail tail))
  489.        (if (null? l)
  490.          result
  491.          (begin
  492.            (set-cdr! ntail (car l))
  493.            (lp (cdr l) (last-pair ntail))))))))))
  494.  
  495.  
  496. (define (append-reverse rev-head tail)
  497.   (let lp ((l rev-head) (acc tail))
  498.     (if (null? l)
  499.       acc
  500.       (lp (cdr l) (cons (car l) acc)))))
  501.  
  502. (define (append-reverse! rev-head tail)
  503.   (append-reverse rev-head tail))    ; XXX:optimize
  504.  
  505. (define (zip clist1 . rest)
  506.   (let lp ((l (cons clist1 rest)) (acc '()))
  507.     (if (any null? l)
  508.       (reverse! acc)
  509.       (lp (map1 cdr l) (cons (map1 car l) acc)))))
  510.  
  511.  
  512. (define (unzip1 l)
  513.   (map1 first l))
  514. (define (unzip2 l)
  515.   (values (map1 first l) (map1 second l)))
  516. (define (unzip3 l)
  517.   (values (map1 first l) (map1 second l) (map1 third l)))
  518. (define (unzip4 l)
  519.   (values (map1 first l) (map1 second l) (map1 third l) (map1 fourth l)))
  520. (define (unzip5 l)
  521.   (values (map1 first l) (map1 second l) (map1 third l) (map1 fourth l)
  522.       (map1 fifth l)))
  523.  
  524. (define (count pred clist1 . rest)
  525.   (if (null? rest)
  526.       (count1 pred clist1)
  527.       (let lp ((result 0)
  528.            (lists (cons clist1 rest)))
  529.     (cond ((any1 null? lists)
  530.            result)
  531.           (else
  532.            (if (apply pred (map1 car lists))
  533.          (lp (1+ result) (map1 cdr lists))
  534.          (lp result      (map1 cdr lists))))))))
  535.  
  536. (define (count1 pred clist)
  537.   (let lp ((result 0) (rest clist))
  538.     (if (null? rest)
  539.     result
  540.     (if (pred (car rest))
  541.         (lp (+ 1 result) (cdr rest))
  542.         (lp result (cdr rest))))))
  543.  
  544. ;;; Fold, unfold & map
  545.  
  546. (define (fold kons knil list1 . rest)
  547.   (if (null? rest)
  548.       (let f ((knil knil) (list1 list1))
  549.     (if (null? list1)
  550.         knil
  551.         (f (kons (car list1) knil) (cdr list1))))
  552.       (let f ((knil knil) (lists (cons list1 rest)))
  553.     (if (any null? lists)
  554.         knil
  555.         (let ((cars (map1 car lists))
  556.           (cdrs (map1 cdr lists)))
  557.           (f (apply kons (append! cars (list knil))) cdrs))))))
  558.  
  559. (define (fold-right kons knil clist1 . rest)
  560.   (if (null? rest)
  561.     (let f ((list1 clist1))
  562.       (if (null? list1)
  563.     knil
  564.     (kons (car list1) (f (cdr list1)))))
  565.     (let f ((lists (cons clist1 rest)))
  566.       (if (any null? lists)
  567.     knil
  568.     (apply kons (append! (map1 car lists) (list (f (map1 cdr lists)))))))))
  569.  
  570. (define (pair-fold kons knil clist1 . rest)
  571.   (if (null? rest)
  572.       (let f ((knil knil) (list1 clist1))
  573.     (if (null? list1)
  574.         knil
  575.         (let ((tail (cdr list1)))
  576.         (f (kons list1 knil) tail))))
  577.       (let f ((knil knil) (lists (cons clist1 rest)))
  578.     (if (any null? lists)
  579.         knil
  580.         (let ((tails (map1 cdr lists)))
  581.           (f (apply kons (append! lists (list knil))) tails))))))
  582.  
  583.  
  584. (define (pair-fold-right kons knil clist1 . rest)
  585.   (if (null? rest)
  586.     (let f ((list1 clist1))
  587.       (if (null? list1)
  588.     knil
  589.     (kons list1 (f (cdr list1)))))
  590.     (let f ((lists (cons clist1 rest)))
  591.       (if (any null? lists)
  592.     knil
  593.     (apply kons (append! lists (list (f (map1 cdr lists)))))))))
  594.  
  595. (define (unfold p f g seed . rest)
  596.   (let ((tail-gen (if (pair? rest)
  597.               (if (pair? (cdr rest))
  598.               (scm-error 'wrong-number-of-args
  599.                      "unfold" "too many arguments" '() '())
  600.               (car rest))
  601.               (lambda (x) '()))))
  602.     (let uf ((seed seed))
  603.       (if (p seed)
  604.       (tail-gen seed)
  605.       (cons (f seed)
  606.         (uf (g seed)))))))
  607.  
  608. (define (unfold-right p f g seed . rest)
  609.   (let ((tail (if (pair? rest)
  610.           (if (pair? (cdr rest))
  611.               (scm-error 'wrong-number-of-args
  612.                      "unfold-right" "too many arguments" '()
  613.                      '())
  614.               (car rest))
  615.               '())))
  616.     (let uf ((seed seed) (lis tail))
  617.       (if (p seed)
  618.       lis
  619.       (uf (g seed) (cons (f seed) lis))))))
  620.  
  621. (define (reduce f ridentity lst)
  622.   (if (null? lst)
  623.       ridentity
  624.       (fold f (car lst) (cdr lst))))
  625.  
  626. (define (reduce-right f ridentity lst)
  627.   (if (null? lst)
  628.       ridentity
  629.       (fold-right f (last lst) (drop-right lst 1))))
  630.  
  631.  
  632. ;; Internal helper procedure.  Map `f' over the single list `ls'.
  633. ;;
  634. (define (map1 f ls)
  635.   (if (null? ls)
  636.       ls
  637.       (let ((ret (list (f (car ls)))))
  638.         (let lp ((ls (cdr ls)) (p ret))         ; tail pointer
  639.           (if (null? ls)
  640.               ret
  641.               (begin
  642.                 (set-cdr! p (list (f (car ls))))
  643.                 (lp (cdr ls) (cdr p))))))))
  644.  
  645. ;; This `map' is extended from the standard `map'.  It allows argument
  646. ;; lists of different length, so that the shortest list determines the
  647. ;; number of elements processed.
  648. ;;
  649. (define (map f list1 . rest)
  650.   (if (null? rest)
  651.     (map1 f list1)
  652.     (let lp ((l (cons list1 rest))
  653.          (rl '()))
  654.       (if (any1 null? l)
  655.     (reverse! rl)
  656.     (lp (map1 cdr l) (cons (apply f (map1 car l)) rl))))))
  657.  
  658. ;; extended to lists of unequal length.
  659. (define map-in-order map)
  660.  
  661. ;; This `for-each' is extended from the standard `for-each'.  It
  662. ;; allows argument lists of different length, so that the shortest
  663. ;; list determines the number of elements processed.
  664. ;;
  665. (define (for-each f list1 . rest)
  666.   (if (null? rest)
  667.     (let lp ((l list1))
  668.       (if (null? l)
  669.     (if #f #f)            ; Return unspecified value.
  670.     (begin
  671.       (f (car l))
  672.       (lp (cdr l)))))
  673.     (let lp ((l (cons list1 rest)))
  674.       (if (any1 null? l)
  675.     (if #f #f)
  676.     (begin
  677.       (apply f (map1 car l))
  678.       (lp (map1 cdr l)))))))
  679.  
  680.  
  681. (define (append-map f clist1 . rest)
  682.   (apply append (apply map f clist1 rest)))
  683.   
  684. (define (append-map! f clist1 . rest)
  685.   (apply append! (apply map f clist1 rest)))
  686.  
  687. (define (map! f list1 . rest)
  688.   (if (null? rest)
  689.       (let lp ((l list1))
  690.     (if (null? l)
  691.         list1
  692.         (begin
  693.           (set-car! l (f (car l)))
  694.           (lp (cdr l)))))
  695.       (let ((res (cons 123 list1)))
  696.     (let lp ((l (cons list1 rest)) (endcell res))
  697.       (if (any1 null? l)
  698.           (begin
  699.         (set-cdr! endcell '()) ;; in case list1 was not the shortest
  700.         (cdr res))
  701.           (begin
  702.         (set-car! (car l) (apply f (map1 car l)))
  703.         (lp (map1 cdr l) (car l))))))))
  704.  
  705. (define (pair-for-each f clist1 . rest)
  706.   (if (null? rest)
  707.     (let lp ((l clist1))
  708.       (if (null? l)
  709.     (if #f #f)
  710.     (begin
  711.       (f l)
  712.       (lp (cdr l)))))
  713.     (let lp ((l (cons clist1 rest)))
  714.       (if (any1 null? l)
  715.     (if #f #f)
  716.     (begin
  717.       (apply f l)
  718.       (lp (map1 cdr l)))))))
  719.  
  720. (define (filter-map f clist1 . rest)
  721.   (if (null? rest)
  722.     (let lp ((l clist1)
  723.          (rl '()))
  724.       (if (null? l)
  725.     (reverse! rl)
  726.     (let ((res (f (car l))))
  727.       (if res
  728.         (lp (cdr l) (cons res rl))
  729.         (lp (cdr l) rl)))))
  730.     (let lp ((l (cons clist1 rest))
  731.          (rl '()))
  732.       (if (any1 null? l)
  733.     (reverse! rl)
  734.     (let ((res (apply f (map1 car l))))
  735.       (if res
  736.         (lp (map1 cdr l) (cons res rl))
  737.         (lp (map1 cdr l) rl)))))))
  738.  
  739. ;;; Filtering & partitioning
  740.  
  741. (define (filter pred list)
  742.   (check-arg-type list? list "filter")  ; reject circular lists.
  743.   (letrec ((filiter (lambda (pred rest result)
  744.               (if (null? rest)
  745.               (reverse! result)
  746.               (filiter pred (cdr rest)
  747.                    (cond ((pred (car rest))
  748.                       (cons (car rest) result))
  749.                      (else
  750.                       result)))))))
  751.     (filiter pred list '())))
  752.  
  753. (define (partition pred list)
  754.   (let lp ((list list)
  755.        (in '())
  756.        (out '()))
  757.     (if (null? list)
  758.     (values (reverse! in) (reverse! out))
  759.     (if (pred (car list))
  760.         (lp (cdr list) (cons (car list) in) out)
  761.         (lp (cdr list) in (cons (car list) out))))))
  762.  
  763. (define (remove pred list)
  764.   (filter (lambda (x) (not (pred x))) list))
  765.  
  766. (define (filter! pred list)
  767.   (filter pred list))            ; XXX:optimize
  768.  
  769. (define (partition! pred list)
  770.   (partition pred list))        ; XXX:optimize
  771.  
  772. (define (remove! pred list)
  773.   (remove pred list))            ; XXX:optimize
  774.  
  775. ;;; Searching
  776.  
  777. (define (find pred clist)
  778.   (if (null? clist)
  779.     #f
  780.     (if (pred (car clist))
  781.       (car clist)
  782.       (find pred (cdr clist)))))
  783.  
  784. (define (find-tail pred clist)
  785.   (if (null? clist)
  786.     #f
  787.     (if (pred (car clist))
  788.       clist
  789.       (find-tail pred (cdr clist)))))
  790.  
  791. (define (take-while pred ls)
  792.   (cond ((null? ls) '())
  793.         ((not (pred (car ls))) '())
  794.         (else
  795.          (let ((result (list (car ls))))
  796.            (let lp ((ls (cdr ls)) (p result))
  797.              (cond ((null? ls) result)
  798.                    ((not (pred (car ls))) result)
  799.                    (else
  800.                     (set-cdr! p (list (car ls)))
  801.                     (lp (cdr ls) (cdr p)))))))))
  802.  
  803. (define (take-while! pred clist)
  804.   (take-while pred clist))        ; XXX:optimize
  805.  
  806. (define (drop-while pred clist)
  807.   (if (null? clist)
  808.     '()
  809.     (if (pred (car clist))
  810.       (drop-while pred (cdr clist))
  811.       clist)))
  812.  
  813. (define (span pred clist)
  814.   (let lp ((clist clist) (rl '()))
  815.     (if (and (not (null? clist))
  816.          (pred (car clist)))
  817.     (lp (cdr clist) (cons (car clist) rl))
  818.     (values (reverse! rl) clist))))
  819.  
  820. (define (span! pred list)
  821.   (span pred list))            ; XXX:optimize
  822.  
  823. (define (break pred clist)
  824.   (let lp ((clist clist) (rl '()))
  825.     (if (or (null? clist)
  826.         (pred (car clist)))
  827.     (values (reverse! rl) clist)
  828.     (lp (cdr clist) (cons (car clist) rl)))))
  829.  
  830. (define (break! pred list)
  831.   (break pred list))            ; XXX:optimize
  832.  
  833. (define (any pred ls . lists)
  834.   (if (null? lists)
  835.       (any1 pred ls)
  836.       (let lp ((lists (cons ls lists)))
  837.     (cond ((any1 null? lists)
  838.            #f)
  839.           ((any1 null? (map1 cdr lists))
  840.            (apply pred (map1 car lists)))
  841.           (else
  842.            (or (apply pred (map1 car lists)) (lp (map1 cdr lists))))))))
  843.  
  844. (define (any1 pred ls)
  845.   (let lp ((ls ls))
  846.     (cond ((null? ls)
  847.        #f)
  848.       ((null? (cdr ls))
  849.        (pred (car ls)))
  850.       (else
  851.        (or (pred (car ls)) (lp (cdr ls)))))))
  852.  
  853. (define (every pred ls . lists)
  854.   (if (null? lists)
  855.       (every1 pred ls)
  856.       (let lp ((lists (cons ls lists)))
  857.     (cond ((any1 null? lists)
  858.            #t)
  859.           ((any1 null? (map1 cdr lists))
  860.            (apply pred (map1 car lists)))
  861.           (else
  862.            (and (apply pred (map1 car lists)) (lp (map1 cdr lists))))))))
  863.  
  864. (define (every1 pred ls)
  865.   (let lp ((ls ls))
  866.     (cond ((null? ls)
  867.        #t)
  868.       ((null? (cdr ls))
  869.        (pred (car ls)))
  870.       (else
  871.        (and (pred (car ls)) (lp (cdr ls)))))))
  872.  
  873. (define (list-index pred clist1 . rest)
  874.   (if (null? rest)
  875.     (let lp ((l clist1) (i 0))
  876.       (if (null? l)
  877.     #f
  878.     (if (pred (car l))
  879.       i
  880.       (lp (cdr l) (+ i 1)))))
  881.     (let lp ((lists (cons clist1 rest)) (i 0))
  882.       (cond ((any1 null? lists)
  883.          #f)
  884.         ((apply pred (map1 car lists)) i)
  885.         (else
  886.          (lp (map1 cdr lists) (+ i 1)))))))
  887.  
  888. (define (member x list . rest)
  889.   (let ((l= (if (pair? rest) (car rest) equal?)))
  890.     (let lp ((l list))
  891.       (if (null? l)
  892.     #f
  893.     (if (l= x (car l))
  894.       l
  895.       (lp (cdr l)))))))
  896.  
  897. ;;; Deletion
  898.  
  899. (define (delete x list . rest)
  900.   (let ((l= (if (pair? rest) (car rest) equal?)))
  901.     (let lp ((l list) (rl '()))
  902.       (if (null? l)
  903.     (reverse! rl)
  904.     (if (l= x (car l))
  905.       (lp (cdr l) rl)
  906.       (lp (cdr l) (cons (car l) rl)))))))
  907.  
  908. (define (delete! x list . rest)
  909.   (let ((l= (if (pair? rest) (car rest) equal?)))
  910.     (delete x list l=)))        ; XXX:optimize
  911.  
  912. (define (delete-duplicates list . rest)
  913.   (let ((l= (if (pair? rest) (car rest) equal?)))
  914.     (let lp ((list list) (rl '()))
  915.       (if (null? list)
  916.     (reverse! rl)
  917.     (lp (delete (car list) (cdr list) l=) (cons (car list) rl))))))
  918.  
  919. (define (delete-duplicates! list . rest)
  920.   (let ((l= (if (pair? rest) (car rest) equal?)))
  921.     (delete-duplicates list l=)))    ; XXX:optimize
  922.  
  923. ;;; Association lists
  924.  
  925. (define (assoc key alist . rest)
  926.   (let ((k= (if (pair? rest) (car rest) equal?)))
  927.     (let lp ((a alist))
  928.       (if (null? a)
  929.     #f
  930.     (if (k= key (caar a))
  931.       (car a)
  932.       (lp (cdr a)))))))
  933.  
  934. (define (alist-cons key datum alist)
  935.   (acons key datum alist))
  936.  
  937. (define (alist-copy alist)
  938.   (let lp ((a alist)
  939.        (rl '()))
  940.     (if (null? a)
  941.       (reverse! rl)
  942.       (lp (cdr a) (acons (caar a) (cdar a) rl)))))
  943.  
  944. (define (alist-delete key alist . rest)
  945.   (let ((k= (if (pair? rest) (car rest) equal?)))
  946.     (let lp ((a alist) (rl '()))
  947.       (if (null? a)
  948.     (reverse! rl)
  949.     (if (k= key (caar a))
  950.       (lp (cdr a) rl)
  951.       (lp (cdr a) (cons (car a) rl)))))))
  952.  
  953. (define (alist-delete! key alist . rest)
  954.   (let ((k= (if (pair? rest) (car rest) equal?)))
  955.     (alist-delete key alist k=)))    ; XXX:optimize
  956.  
  957. ;;; Set operations on lists
  958.  
  959. (define (lset<= = . rest)
  960.   (if (null? rest)
  961.     #t
  962.     (let lp ((f (car rest)) (r (cdr rest)))
  963.       (or (null? r)
  964.       (and (every (lambda (el) (member el (car r) =)) f)
  965.            (lp (car r) (cdr r)))))))
  966.  
  967. (define (lset= = . rest)
  968.   (if (null? rest)
  969.     #t
  970.     (let lp ((f (car rest)) (r (cdr rest)))
  971.       (or (null? r)
  972.       (and (every (lambda (el) (member el (car r) =)) f)
  973.            (every (lambda (el) (member el f (lambda (x y) (= y x)))) (car r))
  974.            (lp (car r) (cdr r)))))))
  975.  
  976. ;; It's not quite clear if duplicates among the `rest' elements are meant to
  977. ;; be cast out.  The spec says `=' is called as (= lstelem restelem),
  978. ;; suggesting perhaps not, but the reference code shows the "list" at each
  979. ;; stage as including those elements already added.  The latter corresponds
  980. ;; to what's described for lset-union, so that's what's done.
  981. ;;
  982. (define (lset-adjoin = list . rest)
  983.   (let lp ((l rest) (acc list))
  984.     (if (null? l)
  985.       acc
  986.       (if (member (car l) acc (lambda (x y) (= y x)))
  987.     (lp (cdr l) acc)
  988.     (lp (cdr l) (cons (car l) acc))))))
  989.  
  990. (define (lset-union = . rest)
  991.   (let ((acc '()))
  992.     (for-each (lambda (lst)
  993.         (if (null? acc)
  994.             (set! acc lst)
  995.             (for-each (lambda (elem)
  996.                 (if (not (member elem acc
  997.                          (lambda (x y) (= y x))))
  998.                     (set! acc (cons elem acc))))
  999.                   lst)))
  1000.           rest)
  1001.     acc))
  1002.  
  1003. (define (lset-intersection = list1 . rest)
  1004.   (let lp ((l list1) (acc '()))
  1005.     (if (null? l)
  1006.       (reverse! acc)
  1007.       (if (every (lambda (ll) (member (car l) ll =)) rest)
  1008.     (lp (cdr l) (cons (car l) acc))
  1009.     (lp (cdr l) acc)))))
  1010.  
  1011. (define (lset-difference = list1 . rest)
  1012.   (if (null? rest)
  1013.     list1
  1014.     (let lp ((l list1) (acc '()))
  1015.       (if (null? l)
  1016.     (reverse! acc)
  1017.     (if (any (lambda (ll) (member (car l) ll =)) rest)
  1018.       (lp (cdr l) acc)
  1019.       (lp (cdr l) (cons (car l) acc)))))))
  1020.  
  1021. ;(define (fold kons knil list1 . rest)
  1022.  
  1023. (define (lset-xor = . rest)
  1024.   (fold (lambda (lst res)
  1025.       (let lp ((l lst) (acc '()))
  1026.         (if (null? l)
  1027.           (let lp0 ((r res) (acc acc))
  1028.         (if (null? r)
  1029.           (reverse! acc)
  1030.           (if (member (car r) lst =)
  1031.             (lp0 (cdr r) acc)
  1032.             (lp0 (cdr r) (cons (car r) acc)))))
  1033.           (if (member (car l) res =)
  1034.         (lp (cdr l) acc)
  1035.         (lp (cdr l) (cons (car l) acc))))))
  1036.     '()
  1037.     rest))
  1038.  
  1039. (define (lset-diff+intersection = list1 . rest)
  1040.   (let lp ((l list1) (accd '()) (acci '()))
  1041.     (if (null? l)
  1042.       (values (reverse! accd) (reverse! acci))
  1043.       (let ((appears (every (lambda (ll) (member (car l) ll =)) rest)))
  1044.     (if appears
  1045.       (lp (cdr l) accd (cons (car l) acci))
  1046.       (lp (cdr l) (cons (car l) accd) acci))))))
  1047.  
  1048.  
  1049. (define (lset-union! = . rest)
  1050.   (apply lset-union = rest))        ; XXX:optimize
  1051.  
  1052. (define (lset-intersection! = list1 . rest)
  1053.   (apply lset-intersection = list1 rest)) ; XXX:optimize
  1054.  
  1055. (define (lset-difference! = list1 . rest)
  1056.   (apply lset-difference = list1 rest))    ; XXX:optimize
  1057.  
  1058. (define (lset-xor! = . rest)
  1059.   (apply lset-xor = rest))        ; XXX:optimize
  1060.  
  1061. (define (lset-diff+intersection! = list1 . rest)
  1062.   (apply lset-diff+intersection = list1 rest)) ; XXX:optimize
  1063.  
  1064. ;;; srfi-1.scm ends here
  1065.