| 1 |
;;; do optimize fx< fx<= fx= fx> fx>= |
| 2 |
;;; do optimize fxzero? |
| 3 |
;;; do optimize fxadd1 fxsub1 fx+ fx- |
| 4 |
(module (s-car s-cdr (s-cons make-stream) stream? s-head) |
| 5 |
(import scheme) |
| 6 |
(define-record stream (car cdr)) |
| 7 |
(define (s-car x) |
| 8 |
(if (stream? x) |
| 9 |
((stream-car x)) |
| 10 |
(error 's-car "~s is not a stream" x))) |
| 11 |
(define (s-cdr x) |
| 12 |
(if (stream? x) |
| 13 |
((stream-cdr x)) |
| 14 |
(error 's-cdr "~s is not a stream" x))) |
| 15 |
(define-syntax s-cons |
| 16 |
(syntax-rules () |
| 17 |
[(_ a d) (make-stream (lambda () a) (lambda () d))])) |
| 18 |
(define (s-head n s) |
| 19 |
(unless (and (fixnum? n) (fx>= n 0)) |
| 20 |
(error 's-head |
| 21 |
"length must be a non-negative fixnum, got ~s" |
| 22 |
n)) |
| 23 |
(unless (stream? s) |
| 24 |
(error 's-head "~s is not a stream" s)) |
| 25 |
(let f ([n n] [s s] [ac '()]) |
| 26 |
(cond |
| 27 |
[(fxzero? n) (reverse ac)] |
| 28 |
[else |
| 29 |
(f (fx- n 1) (s-cdr s) (cons (s-car s) ac))])))) |
| 30 |
|
| 31 |
|
| 32 |
(define (going-up x lim) |
| 33 |
(cond ((= x lim) (s-cons x (going-down (- x 1) (+ lim 1)))) |
| 34 |
(else (s-cons x (going-up (+ x 1) lim))))) |
| 35 |
(define (going-down x lim) |
| 36 |
(cond ((= x 0) (going-up 1 lim)) |
| 37 |
(else (s-cons x (going-down (- x 1) lim))))) |
| 38 |
(define s (going-up 1 1)) |
| 39 |
|
| 40 |
#!eof |
| 41 |
|
| 42 |
Path: g2news2.google.com!news3.google.com!newshub.sdsu.edu!tethys.csu.net!okeanos.csu.net!53ab2750!not-for-mail |
| 43 |
Sender: [email protected] |
| 44 |
Newsgroups: comp.lang.scheme |
| 45 |
Subject: Re: Stream problem |
| 46 |
References: <[email protected]> |
| 47 |
From: Andru Luvisi <[email protected]> |
| 48 |
Message-ID: <[email protected]> |
| 49 |
Lines: 21 |
| 50 |
User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.4 |
| 51 |
MIME-Version: 1.0 |
| 52 |
Content-Type: text/plain; charset=us-ascii |
| 53 |
Date: 05 Dec 2006 13:53:31 -0800 |
| 54 |
NNTP-Posting-Host: 130.157.65.249 |
| 55 |
X-Trace: okeanos.csu.net 1165355611 130.157.65.249 (Tue, 05 Dec 2006 13:53:31 PST) |
| 56 |
NNTP-Posting-Date: Tue, 05 Dec 2006 13:53:31 PST |
| 57 |
|
| 58 |
|
| 59 |
Several of the answers that have been posted involve using reverse, |
| 60 |
which has the disadvantage that the later you go in the stream, the |
| 61 |
more of it you will need to hold in ram if you are processing and |
| 62 |
discarding elements as you go. |
| 63 |
|
| 64 |
(define (going-up x lim) |
| 65 |
(cond ((= x lim) (s-cons x (going-down (- x 1) (+ lim 1)))) |
| 66 |
(else (s-cons x (going-up (+ x 1) lim))))) |
| 67 |
(define (going-down x lim) |
| 68 |
(cond ((= x 0) (going-up 1 lim)) |
| 69 |
(else (s-cons x (going-down (- x 1) lim))))) |
| 70 |
(define s (going-up 1 1)) |
| 71 |
|
| 72 |
Andru |
| 73 |
-- |
| 74 |
Andru Luvisi |
| 75 |
|
| 76 |
Quote Of The Moment: |
| 77 |
Quidquid latine dictum sit, altum viditur. |
| 78 |
( Whatever is said in Latin sounds profound. ) |