util.scm 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. ; -*- Mode: Scheme; Syntax: Scheme; Package: Scheme; -*-
  2. ; Copyright (c) 1993-2008 by Richard Kelsey and Jonathan Rees. See file COPYING.
  3. ; This is file util.scm.
  4. ;;;; Utilities
  5. (define (unspecific) (if #f #f))
  6. (define (reduce cons nil list) ;used by length, append, etc.
  7. (if (null? list)
  8. nil
  9. (cons (car list) (reduce cons nil (cdr list)))))
  10. (define (filter pred lst)
  11. (reduce (lambda (x rest)
  12. (if (pred x) (cons x rest) rest))
  13. '()
  14. lst))
  15. ; Position of an object within a list
  16. (define (pos pred)
  17. (lambda (thing l)
  18. (let loop ((i 0) (l l))
  19. (cond ((null? l) #f)
  20. ((pred thing (car l)) i)
  21. (else (loop (+ i 1) (cdr l)))))))
  22. (define posq (pos eq?))
  23. (define posv (pos eqv?))
  24. (define posqual (pos equal?))
  25. ; Is pred true of any element of l?
  26. (define (any pred l)
  27. ;; (reduce or #f l), sort of
  28. (if (null? l)
  29. #f
  30. (or (pred (car l))
  31. (any pred (cdr l)))))
  32. ; Is pred true of every element of l?
  33. (define (every pred l)
  34. ;; (reduce and #t l), sort of
  35. (if (null? l)
  36. #t
  37. (and (pred (car l)) (every pred (cdr l)))))
  38. (define (sublist l start end)
  39. (if (> start 0)
  40. (sublist (cdr l) (- start 1) (- end 1))
  41. (let recur ((l l) (end end))
  42. (if (= end 0)
  43. '()
  44. (cons (car l) (recur (cdr l) (- end 1)))))))
  45. (define (last x)
  46. (if (null? (cdr x))
  47. (car x)
  48. (last (cdr x))))
  49. (define (insert x l <)
  50. (cond ((null? l) (list x))
  51. ((< x (car l)) (cons x l))
  52. (else (cons (car l) (insert x (cdr l) <)))))
  53. (define (symbol-append . syms)
  54. (string->symbol (apply string-append
  55. (map symbol->string syms))))
  56. ;----------------
  57. ; Variations on a theme.
  58. ;
  59. ; FOLD is a tail-recursive version of REDUCE.
  60. (define (fold folder list accumulator)
  61. (do ((list list (cdr list))
  62. (accum accumulator (folder (car list) accum)))
  63. ((null? list)
  64. accum)))
  65. (define (fold->2 folder list acc0 acc1)
  66. (let loop ((list list) (acc0 acc0) (acc1 acc1))
  67. (if (null? list)
  68. (values acc0 acc1)
  69. (call-with-values
  70. (lambda ()
  71. (folder (car list) acc0 acc1))
  72. (lambda (acc0 acc1)
  73. (loop (cdr list) acc0 acc1))))))
  74. (define (fold->3 folder list acc0 acc1 acc2)
  75. (let loop ((list list) (acc0 acc0) (acc1 acc1) (acc2 acc2))
  76. (if (null? list)
  77. (values acc0 acc1 acc2)
  78. (call-with-values
  79. (lambda ()
  80. (folder (car list) acc0 acc1 acc2))
  81. (lambda (acc0 acc1 acc2)
  82. (loop (cdr list) acc0 acc1 acc2))))))
  83. ;----------------
  84. ; A version of LET and LET* which allows clauses that return multiple values.
  85. ;
  86. ; There is another copy of this in big/mvlet.scm.
  87. ;
  88. ; MV = multiple-value
  89. ;
  90. ; (mvlet (<clause> ...) <body>)
  91. ; (mvlet* (<clause> ...) <body>)
  92. ;
  93. ; <clause> ::= (<ids> <expression>)
  94. ; <ids> ::= <id> | (<id> ...) | (<id> ... . <id>)
  95. ;
  96. ; A clause of the form (<id> <exp>) is like a normal LET clause. There is no
  97. ; clause equivalent to
  98. ; (call-with-values (lambda () <expression>)
  99. ; (lambda <id> <body>))
  100. (define-syntax mvlet
  101. (syntax-rules ()
  102. ((mvlet () body ...)
  103. (let () body ...))
  104. ((mvlet (clause ...) body ...)
  105. (mvlet-helper (clause ...) () (body ...)))))
  106. (define-syntax mvlet-helper
  107. (syntax-rules ()
  108. ((mvlet-helper () clauses (body ...))
  109. (let clauses body ...))
  110. ((mvlet-helper (((var . more-vars) val) more ...) clauses body)
  111. (copy-vars (var . more-vars) () val (more ...) clauses body))
  112. ((mvlet-helper ((var val) more ...) clauses body)
  113. (mvlet-helper (more ...) ((var val) . clauses) body))))
  114. (define-syntax copy-vars
  115. (syntax-rules ()
  116. ((copy-vars (var . more-vars) (copies ...)
  117. val more clauses body)
  118. (copy-vars more-vars (copies ... x)
  119. val more ((var x) . clauses) body))
  120. ((copy-vars () copies val more clauses body)
  121. (call-with-values
  122. (lambda () val)
  123. (lambda copies
  124. (mvlet-helper more clauses body))))
  125. ((copy-vars last (copies ...) val more clauses body)
  126. (call-with-values
  127. (lambda () val)
  128. (lambda (copies ... . lastx)
  129. (mvlet-helper more ((last lastx) . clauses) body))))))
  130. (define-syntax mvlet*
  131. (syntax-rules ()
  132. ((mvlet* () body ...)
  133. (let () body ...))
  134. ((mvlet* (((vars ...) val) clause ...) body ...)
  135. (call-with-values
  136. (lambda () val)
  137. (lambda (vars ...)
  138. (mvlet (clause ...) body ...))))
  139. ((mvlet* ((var val) clause ...) body ...)
  140. (let ((var val)) (mvlet (clause ...) body ...)))))