#lang racket ; based on SICP section 3.5, but shorter and more consistent names ; implementation of stream primative operations used to implement all additional ; stream behaviour as defined in the sbase.rkt . ; By redefining these you can have different time/space complexity implementation of streams. (provide s-cons) (provide s-first) (provide s-rest) (provide s-empty) (provide s-null?) ; Implementation 1: ; immediate s-first, delayed s-rest, using delay/force ; streams look like lists (thus the s-first, s-rest operations), ; but are implemented as dotted pairs, where the first element ; is the value at the head of the list, and the second is a promise ; to generate the rest of the list. (define-syntax-rule (s-cons a b) (cons a (delay b))) (define (s-first s) (car s)) ; ; force an evaluate of the next term if you want the rest of the stream. (define (s-rest s) (force (cdr s))) (define s-empty '()) (define s-null? null?) ; Implementation 2: ; delayed s-first and s-rest, using delay/force ; (define-syntax-rule (s-cons a b) (cons (delay a) (delay b))) ; (define (s-first s) (force (car s))) ; (define (s-rest s) (force (cdr s))) ; (define s-empty '()) ; (define s-null? null?) ; Implementation 3: ; re-compute every time, no delay/force ; (define-syntax-rule (s-cons a b) (cons (lambda () a) (lambda () b))) ; (define (s-first s) ((car s)) ) ; (define (s-rest s) ((cdr s)) ) ; (define s-empty '()) ; (define s-null? null?)