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)

 Instructor: Geoffrey Mainland mainland+cs360@drexel.edu 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 ().

Example:

## 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!

Example:

## 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.

Examples:

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.

Examples:

## 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.

Example:

## 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.