;; starting code for Homework 18 exercises ;; ;; this code is written in Ruse ;; ;;----------------------------------------------------------------- ;; we wrote this code in class ;; 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)))) ;; creates a stream of ascending numbers beginning with n (def ascending-stream (fun (n) : (zcons n (ascending-stream (n + 1))))) ;; 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))) ;; combines corresponding elements of two streams using addition (def add-streams (fun (s1 s2) : (zcons ((zcar s1) + (zcar s2)) (add-streams (zcdr s1) (zcdr s2))))) ;; combines corresponding elements of two streams using multiplication (def multiply-streams (fun (s1 s2) : (zcons ((zcar s1) * (zcar s2)) (multiply-streams (zcdr s1) (zcdr s2))))) ;;----------------------------------------------------------------- ;; your new Ruse code goes here