CS 360
Winter 2016
Programming Language Concepts
CS 360-001 Tuesday/Thursday 15:30-16:50 (Rush 014)
CS 360-002 Tuesday/Thursday 14:00-15:20 (Rush 014)
CS 360-003 Tuesday 18:30-21:20 (University Crossings 153)

Geoffrey Mainland
Office: University Crossings 106
Office hours: Mondays 3pm–5pm; Thursdays 5pm–6pm.
Teaching Assistants:
Pavan Kantharaju
Matthew Roll
Warning! This material is for an old version of the course.

This assignment asks you to write several Scheme functions that make use of state and streams. Implement, document (i.e., write a specification for), and test the following functions in scheme. Scheme includes some of the constructs below (like association lists), but you may not use these implementations. Instead, you must implement them yourself. Functions that manipulate streams may not use lists internally. We will test your code with infinite streams. Make sure all functions are thoroughly tested.

You must implement the functions as specified. You may write other helper functions and define test data in your file, but you may not change the functions’ names or the number or order of arguments. You must also write a short description of what the function does as we did for the examples in class.

You should complete the homework by modifying the file hw2.rkt that we provide as part of the solution template. Please put a version of hw2.rkt containing your changes in the hw2 subdirectory of your git repository.

This assignment is worth 60 points. There are 61 possible points.

Standard homework instructions

Your code must run either on tux or on the course VM using the version of Racket that we provide.

A solution template is available in the DrexelCS360/homeworks GitHub repository. You should only need to modify the files provided by the template—please do not check in any files beyond those that the template provides.

You can check that your code compiles by typing make in the hw2 directory. If make does not complete successfully, it means your code does not compile. Code that does not compile will receive a zero.

We have included several test for your convenience. Passing all provided tests does not guarantee full credit, but failing tests does guarantee less than full credit. You can run the tests by typing make run-tests in the hw2 directory.

Please submit all files for this assignment by pushing them to your DrexelCS360 git repository. Be sure to push your work to GitHub. If you do not push to your GitHub repository, we cannot grade your code and you will not receive credit.

If you aren’t sure that your files made it to GitHub, look at your repository on the GitHub web site. What you see there is what we will see.

Problem 1: (iterator rlist) (10 points)

The function iterator returns a function which when repeatedly called, returns the numbers in the sequence (range (list start step end)). When the sequence is exhausted, the returned function should return ().


(define (repeat f n)
  (if (= n 0)
      (cons (f) (repeat f (- n 1)))))
(let ((next (iterator '(0 2 7)))) (repeat next 5)) => (0 2 4 6 ())

Problem 2: (stream-seq f rlist) (10 points)

Modify the seq function you wrote for the previous assignment so that it returns a stream rather than a list.

Note: If you did not receive full credit for your implementation of seq on the last homework, be sure to fix it first!


(stream-first (stream-rest (stream-seq square '(1 1 7)))) => 4

Problem 3: (scan f z l) (10 points)

The scan function scan takes a binary function f, a value z, and a list l, and returns the list $z, f(x_1, z), f(x_2, f(x_1, z)), \ldots, f(x_n, f(x_{n-1}, …))$ where $x_1, x_2, …, x_n$ are the elements of the list l.


(scan + 0 '()) => (0)
(scan + 0 '(1 2 3 4 5 6)) => (0 1 3 6 10 15 21)
(scan * 1 '(1 2 3 4 5 6)) => (1 1 2 6 24 120 720)

This may remind you of the reduce function. You can think of scan as a version of reduce that returns a list of the all the intermediate results of the reduction.

Problem 4: (stream-scan f z s) (10 points)

Modify your scan function so that it takes a stream as an argument and returns a stream as the result—stream-scan must not use lists.

Problem 5: (lookup name alist) (10 points)

An association list is a list of bindings of names to values: ((name1 val1) ... (namen valuen)). This data structure can be used to implement a symbol table.

Write recursive scheme function, lookup, that returns the binding (pair) whose name equals the given name. If no such binding is found, return nil.


(define assocs '((ben "short") (cara "walking") (dan "bald")))
(lookup 'albert assocs) => ()
(lookup 'ben assocs) => (ben "short")

Problem 6: (lookup-env name env) (10 points)

An environment can be represented by a list of association lists (i.e., a list of symbol tables), where the first element in the list is the nearest scope, the second the next surrounding scope, and the last the outermost scope.

Write a recursive function, lookup-env, which returns the binding with the specified name in the nearest scope. If no such binding is found, return nil.


(define l1 '((ben "short") (cara "walking") (dan "bald")))
(define l2 '((albert "is not") (ski "skinny") (kim "cook") (cara "injured")))
(define e (list l1 l2) )

(lookup-env 'ben e) => (ben "short")
(lookup-env 'albert e) => ('albert "is not")
(lookup-env 'cara e) => (cara "walking")
(lookup-env 'eggbert e) => ()

Problem 7: Homework Statistics (1 point)

How long did it take you to complete each problem? Please tell us in a comment in each of the files you submit. You must tell us how long each problem took you to receive the point.