|
|
@@ -6,6 +6,34 @@ |
|
|
|
|
|
|
|
|
(define-syntax (pe expr) `(print ',expr "evaluates to" ,expr)) |
|
|
(define-syntax (pe expr) `(print ',expr "evaluates to" ,expr)) |
|
|
|
|
|
|
|
|
|
|
|
(define the-empty-stream ()) |
|
|
|
|
|
|
|
|
|
|
|
(define (stream-null? s) (if s t ())) |
|
|
|
|
|
|
|
|
|
|
|
(define-syntax (delay expr) `(,lambda () ,expr)) |
|
|
|
|
|
|
|
|
|
|
|
(define (force promise) (promise)) |
|
|
|
|
|
|
|
|
|
|
|
(define (pair-stream oject expression) (pair object (delay expression))) |
|
|
|
|
|
|
|
|
|
|
|
(define stream-first first) |
|
|
|
|
|
|
|
|
|
|
|
(define (stream-rest stream) (force (rest stream))) |
|
|
|
|
|
|
|
|
|
|
|
(define (stream-ref s n) (if (= n 0) (stream-first s) (stream-ref (stream-rest s) (- n 1)))) |
|
|
|
|
|
|
|
|
|
|
|
(define (stream-filter pred stream) (cond ((stream-null? stream) the-empty-stream) ((pred (stream-first stream)) (pair-stream (stream-first stream) (stream-filter pred (stream-rest stream)))) (else (stream-filter pred (stream-rest stream))))) |
|
|
|
|
|
|
|
|
|
|
|
(define (stream-map proc s) (if (stream-null? s) the-empty-stream (pair-stream (proc (stream-first s)) (stream-map proc (stream-rest s))))) |
|
|
|
|
|
|
|
|
|
|
|
(define (stream-for-each proc s) (if (stream-null? s) 'done (begin (proc (stream-first s)) (stream-for-each proc (stream-rest s))))) |
|
|
|
|
|
|
|
|
|
|
|
(define (stream-enumerate-interval low high) (if (> low high) the-empty-stream (pair-stream low (stream-enumerate-interval (+ low 1) high)))) |
|
|
|
|
|
|
|
|
|
|
|
(define (prime? x) (define (prime-helper x k) (cond ((= x k) t) ((= (% x k) 0) ()) (else (prime-helper x (+ k 1))))) (cond ((= x 1) ()) ((= x 2) t) (else (prime-helper x 2)))) |
|
|
|
|
|
|
|
|
|
|
|
(define (a) (stream-first (stream-rest (stream-filter prime? (stream-enumerate-interval 10000 1020))))) |
|
|
|
|
|
|
|
|
(define-syntax (when condition . body) :doc "Special form for when multiple actions should be done if a\ncondition is true.\n\n{{{example_start}}}\n(when (not ())\n (print "Hello ")\n (print "from ")\n (print "when!"))\n\n(when ()\n (print "Goodbye ")\n (print "World!"))\n{{{example_end}}}\n" (if (= (rest body) ()) `(if ,condition (unquote-splicing body) nil) `(if ,condition (begin (unquote-splicing body)) nil))) |
|
|
(define-syntax (when condition . body) :doc "Special form for when multiple actions should be done if a\ncondition is true.\n\n{{{example_start}}}\n(when (not ())\n (print "Hello ")\n (print "from ")\n (print "when!"))\n\n(when ()\n (print "Goodbye ")\n (print "World!"))\n{{{example_end}}}\n" (if (= (rest body) ()) `(if ,condition (unquote-splicing body) nil) `(if ,condition (begin (unquote-splicing body)) nil))) |
|
|
|
|
|
|
|
|
(define-syntax (unless condition . body) :doc "Special form for when multiple actions should be done if a\ncondition is false." (if (= (rest body) ()) `(if ,condition nil (unquote-splicing body)) `(if ,condition nil (begin (unquote-splicing body))))) |
|
|
(define-syntax (unless condition . body) :doc "Special form for when multiple actions should be done if a\ncondition is false." (if (= (rest body) ()) `(if ,condition nil (unquote-splicing body)) `(if ,condition nil (begin (unquote-splicing body))))) |
|
|
|