;; developed in class Tuesday, November 26 ;; this code is written in Ruse ;; returns the nth element of stream s (def nth (fun (n s) : (if (n == 0) then (zcar s) else (nth (n - 1) (zcdr s))))) ;; builds an ordinary list of the first n elements of stream s (def get-first (fun (n s) : (if (n == 0) then nil ;; must use cons here to build an ordinary list else (cons (zcar s) (get-first (n - 1) (zcdr s)))))) ;; prints the first n elements of stream s, one element per line (def print-first (fun (n s) : (if (n == 0) then (print "...") else (do (print (zcar s)) (print-first (n - 1) (zcdr s)))))) ;; creates a stream of constant values of n (def constant-stream (fun (n) : (zcons n (constant-stream n)))) ;; an infinite stream of 5s (def fives (constant-stream 5)) ;; (get-first 15 fives) => (5 5 5 5 5 5 5 5 5 5 5 5 5 5 5) ;; creates a stream of ascending numbers beginning with n (def ascending-stream (fun (n) : (zcons n (ascending-stream (n + 1))))) ;; (get-first 15 (ascending-stream 10)) => (10 11 12 13 14 15 ...) ;; applies a one-argument function f to all elements of stream s (def zmap (fun (f s) : (zcons (f (zcar s)) (zmap f (zcdr s))))) (def add1 (fun (n) : (n + 1))) ;; the stream of natural numbers: (0 1 2 3 4 5 ...) (def nats (zcons 0 (zmap add1 nats))) ;; (get-first 15 nats) => (0 1 2 3 4 5 6 7 8 ...) ;; the stream of squares: (0 1 4 9 16 25 ...) (def squares (zmap square nats)) ;; (get-first 15 squares) => (0 1 4 9 16 25 36 49 ...) (def add-streams (fun (s1 s2) : (zcons ((zcar s1) + (zcar s2)) (add-streams (zcdr s1) (zcdr s2))))) (def multiply-streams (fun (s1 s2) : (zcons ((zcar s1) * (zcar s2)) (multiply-streams (zcdr s1) (zcdr s2))))) ;; another way of creating the stream of squares (def squares (multiply-streams nats nats)) ;; the Fibonacci numbers: 0 1 1 2 3 5 8 13 21 34 55 ... ;; ;; fibs = 0 1 1 2 3 5 8 13 21 34 55 ... ;; + (zcdr fibs) = 1 1 2 3 5 8 13 21 34 55 89 ... ;; ---------------------------------------------------- ;; [0 1] 1 2 3 5 8 13 21 34 55 89 144 ... (def fibs (zcons 0 (zcons 1 (add-streams fibs (zcdr fibs))))) ;; the factorials: 0! 1! 2! 3! 4! 5! 6! ... ;; ;; facts = 1 1 2 6 24 120 720 ... ;; * (zcdr nats) = 1 2 3 4 5 6 7 ... ;; ---------------------------------------------- ;; [1] 1 2 6 24 120 720 5040 ... (def facts (zcons 1 (multiply-streams facts (zcdr nats)))) ;; The factorial function. Conceptually, ! doesn't do any ;; "computation" at all, at least in the traditional sense. ;; It simply picks the appropriate ready-made answer from ;; its infinite collection of answers: (def ! (fun (n) : (nth n facts))) ;; (! 5) => 120 ;; (! 10) => 3628800 ;; the powers of 2: 1 2 4 8 16 32 64 128 256 512 1024 ... ;; ;; powers-of-two = 1 2 4 8 16 32 64 128 ... ;; + powers-of-two = 1 2 4 8 16 32 64 128 ... ;; ----------------------------------------------------- ;; [1] 2 4 8 16 32 64 128 256 ... (def powers-of-two (zcons 1 (add-streams powers-of-two powers-of-two))) (def 2-to-the (fun (n) : (nth n powers-of-two))) ;; (2-to-the 9) => 512 (def random-stream (fun (n) : (zcons (random n) (random-stream n)))) ;; a stream of random numbers in the range 0-99 (def rands (random-stream 100))