File: iter.l

package info (click to toggle)
zenlisp 2013.11.22-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 596 kB
  • sloc: ansic: 2,547; makefile: 55
file content (24 lines) | stat: -rw-r--r-- 759 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
; zenlisp iterators
; By Nils M Holm, 2007, 2008
; Feel free to copy, share, and modify this code.
; See the file LICENSE for details.

(define iter :t)

(define (arithmetic-iterator conv fn neutral)
  (lambda x
    (cond ((null x) neutral)
          (t (fold (lambda (a b)
                     (fn (conv a) (conv b)))
                   (car x)
                   (cdr x))))))

(define (predicate-iterator conv fn)
  (let ((:fail (cons ':fail ())))
    (let ((comp (lambda (a b)
                  (cond ((eq a :fail) :fail)
                        ((fn (conv a) (conv b)) b)
                        (t :fail)))))
      (lambda (first . rest)
        (cond ((null rest) (bottom '(too few arguments)))
              (t (neq (fold comp first rest) :fail)))))))