Commits

Moritz Heidkamp committed 74cd2dc

Implement lazy-reverse

Comments (0)

Files changed (2)

 (lazy-seq make-lazy-seq
  lazy-seq? lazy-seq-realized? lazy-null? 
  lazy-seq->list list->lazy-seq
- lazy-head lazy-tail
- lazy-length lazy-append
+ lazy-head lazy-tail lazy-length
+ lazy-append lazy-reverse
  lazy-take lazy-drop lazy-ref
  lazy-take-while lazy-drop-while
  lazy-map lazy-filter lazy-each
   (lazy-seq
     (cons x (lazy-iterate f (f x)))))
 
+(define (lazy-reverse seq)
+  (let loop ((seq seq) (rev-seq (lazy-seq '())))
+    (lazy-seq
+      (if (lazy-null? seq)
+          rev-seq
+          (loop (lazy-tail seq)
+                (%make-lazy-seq #f (cons (lazy-head seq) rev-seq)))))))
+
 )
         (lazy-seq->list
          (lazy-drop-while number? (list->lazy-seq '(1 2 3 4 end))))))
 
+(test-group "lazy-reverse"
+  (test '() (lazy-seq->list (lazy-reverse (lazy-seq '()))))
+  (test '(3 2 1)
+        (lazy-seq->list
+         (lazy-reverse
+          (lazy-numbers count: 3 start: 1)))))
+
 (test-exit)