seq.scm 682 B

123456789101112131415161718192021
  1. (define (elt? exp)
  2. (and (pair? exp)
  3. (eq? 'elt (car exp))
  4. (pair? (cdr exp))
  5. (null? (cddr exp))))
  6. (define (elt-get-elt exp) (cadr exp))
  7. (define (cat? exp) (and (pair? exp) (eq? 'cat (car exp))))
  8. (define (cat-get-seqs exp) (cdr exp))
  9. (define (seq->dlist seq tail)
  10. (cond ((elt? seq) (cons (elt-get-elt seq) tail))
  11. ((cat? seq) (fold seq->dlist tail (cat-get-seqs seq)))
  12. (else (error 'seq->dlist "?" seq))))
  13. (define (seq->list seq) (seq->dlist seq '()))
  14. (define (seq-length^ seq tail)
  15. (cond ((elt? seq) (+ 1 tail))
  16. ((cat? seq) (fold seq-length^ tail (cat-get-seqs seq)))
  17. (else (error 'seq-length^ "?" seq))))
  18. (define (seq-length seq) (seq-length^ seq 0))