fork download
  1. (define-syntax lazy-cons
  2. (syntax-rules ()
  3. ((_ head tail) (cons (delay head) (delay tail)))))
  4.  
  5. (define (lazy-head ll)
  6. (force (car ll)))
  7.  
  8. (define (lazy-tail ll)
  9. (force (cdr ll)))
  10.  
  11. (define-syntax lazy-list
  12. (syntax-rules ()
  13. ((_ x) (lazy-cons x '()))
  14. ((_ x . xs) (lazy-cons x (lazy-list . xs)))))
  15.  
  16. ;;; ----------------------------------------------------------------
  17.  
  18. (define (lazy-take n xs)
  19. (cond ((<= n 0)
  20. '())
  21. ((null? xs)
  22. '())
  23. (else
  24. (lazy-cons (lazy-head xs) (lazy-take (- n 1) (lazy-tail xs))))))
  25.  
  26. (define (lazy-zip f xs ys)
  27. (cond ((null? xs) '())
  28. ((null? ys) '())
  29. (else (lazy-cons (f (lazy-head xs) (lazy-head ys))
  30. (lazy-zip f (lazy-tail xs) (lazy-tail ys))))))
  31.  
  32. (define (force-list xs)
  33. (if (null? xs)
  34. '()
  35. (cons (lazy-head xs) (force-list (lazy-tail xs)))))
  36.  
  37. ;;; ----------------------------------------------------------------
  38.  
  39. (define fibs (lazy-cons 0 (lazy-cons 1 (lazy-zip + fibs (lazy-tail fibs)))))
  40.  
  41. (print (force-list (lazy-take 10 fibs)))
Success #stdin #stdout 0.01s 7924KB
stdin
Standard input is empty
stdout
(0 1 1 2 3 5 8 13 21 34)