Commits

Anonymous committed 15ecac5

Implemenet lazy-append

  • Participants
  • Parent commits 48ab239

Comments (0)

Files changed (2)

File lazy-seq.scm

  lazy-seq? lazy-seq-realized?
  lazy-null lazy-null?
  lazy-seq->list list->lazy-seq
- lazy-head lazy-tail
+ lazy-head lazy-tail lazy-append
  lazy-take lazy-drop lazy-ref
  lazy-map lazy-filter lazy-each
  lazy-numbers lazy-repeat lazy-repeatedly
- realized-lazy-seq
+ realized-lazy-seq 
  input-port->lazy-seq)
 
 (import chicken scheme)
 (define (lazy-repeatedly f)
   (lazy-seq (cons (f) (lazy-repeatedly f))))
 
+(define (lazy-append . seqs)
+  (let loop ((seqs seqs))
+    (lazy-seq
+      (if (null? seqs)
+          '()
+          (let loop2 ((seq (car seqs)))
+            (lazy-seq
+              (if (lazy-null? seq)
+                  (loop (cdr seqs))
+                  (cons (lazy-head seq)
+                        (loop2 (lazy-tail seq))))))))))
+
 )

File tests/run.scm

   (test '(x x x x) (lazy-seq->list (lazy-take 4 (lazy-repeat 'x)))))
 
 (test-group "lazy-repeatedly"
-  (test 10 (lazy-ref 9 (lazy-repeatedly (let ((n 0)) (lambda () (set! n (+ n 1)) n))))))
+  (test 10 (lazy-ref 9 (lazy-repeatedly
+                        (let ((n 0))
+                          (lambda () (set! n (+ n 1)) n))))))
+
+(test-group "lazy-append"
+  (test 6 (lazy-ref 5 (lazy-append (lazy-numbers count: 2)
+                                   (lazy-numbers count: 2)
+                                   (lazy-numbers start: 5)))))
 
 (test-exit)