CS 2135 Homework 7: Streams and Script Position

Due: February 25 (Tuesday) at 11:59pm via turnin (assignment name hwk7).

Assignment Goals


Exercises: Streams

  1. Write a program make-square-stream that produces an infinite stream of squares (1, 4, 9, 16, ...). Do this without using the stream-cons and stream-rest definitions from class (and don't define similar helpers/macros for yourself). I want to make sure you understand how streams work and are built without relying on these helpers. Assume that all you have is the stream-struct: (define-struct stream (first make-rest)).

Exercises: Script Position

Convert each of the following programs such that all calls to scripts are in script-position.

  1. (define (fscript x) (+ 3 (* 2 x)))
    (define (gscript y z) (+ (* y y) (* z z)))
    (fscript (gscript (fscript 7) 8))
    
  2. (define (square-script x) (* x x))
    (define (tri-height x) (sqrt (- (square-script x) (square-script (* .5 x)))))
    
    (define (shape-area shape-type size)
      (cond [(symbol=? shape-type 'circle) (* pi (square-script size))]
            [(symbol=? shape-type 'square) (square-script size)]
            [(symbol=? shape-type 'equilat-tri) (* size (tri-height size))]))
    
  3. ;; assume that fscript could be rewritten as fscript/web, taking an action
    ;;    as a second argument 
    
    (define (map fscript L)
      (cond [(empty? L) empty]
            [(cons? L) (cons (fscript (first L))
                             (map fscript (rest L)))]))
    
  4. (define (prompt-read promptstr)
      (begin
        (printf "~a: " promptstr)
        (read)))
    
    (define (prompt-script plist)
      (cond [(empty? plist) empty]
    	[(cons? plist)
    	 (cons (prompt-read (first plist))
    	       (prompt-script (rest plist)))]))
    

What to Turn In

Turn in a single file hwk7.ss (or hwk7.scm) containing all code and documentation for this assignment. Make sure that both students' names are in a comment at the top of the file.


Hints and Guidelines


Back to the Assignments page