SHARE
TWEET

Untitled

a guest Nov 18th, 2019 117 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (define-syntax my-delay
  2.   (syntax-rules ()
  3.     ((my-delay x) (lambda () x))))
  4.  
  5. (define-syntax cons-stream
  6.   (syntax-rules ()
  7.     ((cons-stream head tail)(cons head (delay tail)))))
  8.  
  9. (define empty-stream `())
  10. (define (car-stream s)
  11.   (car s))
  12. (define (cdr-stream s)
  13.   (force (cdr s)))
  14.  
  15. (define (nats-after n)
  16.   (cons-stream n (nats-after (+ 1 n))))
  17.  
  18. (define nats (nats-after 0))
  19.  
  20. (define (pow-after n)
  21.   (cons-stream (expt 2 n) (pow-after (+ n 1))))
  22.  
  23. (define pow2 (pow-after 0))
  24.  
  25. (define (fibs-after first second)
  26.   (cons-stream (+ first second) (fibs-after second (+ first second))))
  27.  
  28. (define fibs (fibs-after 0 1))
  29.  
  30. (define (stream-take s n)
  31.   (if (equal? n 0) `()
  32.       (cons (car-stream s) (stream-take (cdr-stream s) (- n 1)))))
  33.  
  34. (define (stream-from-list l)
  35.   (if (null? l) `()
  36.       (cons-stream (car l) (stream-from-list (cdr l)))))
  37. (define l `(1 2 3 4 5 6))
  38.  
  39. (define (stream-map f s)
  40.   (if (null? s) `()
  41.       (cons-stream (f (car-stream s)) (stream-map f (cdr-stream s)))))
  42.  
  43. (define (stream-filter p? s)
  44.   (if (null? s) `()
  45.       (if (p? (car-stream s)) (cons-stream (car-stream s) (stream-filter p? (cdr-stream s)))
  46.           (stream-filter p? (cdr-stream s)))))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top