CS 360
Winter 2015
Programming Language Concepts
Tuesdays, Thursdays 14:00-15:20
University Crossings 151

Instructor:
Geoffrey Mainland
mainland+cs360@cs.drexel.edu
University Crossings 106
Teaching Assistant:
Brian Lee
bl389@drexel.edu
Warning! This material is for an old version of the course.

CS 360 Homework 2: More Scheme

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. Your description must contain examples of the function in operation.

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

Please submit all files for this assignment by checking them in to your cs360 git repository at ~/cs360/git/hw2. Be sure to commit your work to the repository.

A complete submission will include six files:

  1. iterator.scm
  2. stream-seq.scm
  3. scan.scm
  4. stream-scan.scm
  5. lookup.scm
  6. lookup-env.scm

Your code must run on tux under mit-scheme.

Code that is not valid Scheme will not be graded and will receive a score of zero.

Problem 1: (iterator rlist) (10 points total)

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:

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

Submit the solutions to this exercise in a file named iterator.scm in the hw2 subdirectory of your git repository.

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

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:

(stream-car (stream-cdr (stream-seq square '(1 1 7)))) => 4 

Submit the solutions to this exercise in a file named stream-seq.scm in the hw2 subdirectory of your git repository.

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

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:

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

Submit the solutions to this exercise in a file named scan.scm in the hw2 subdirectory of your git repository.

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

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.

Submit the solutions to this exercise in a file named stream-scan.scm in the hw2 subdirectory of your git repository.

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

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:

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

Submit the solutions to this exercise in a file named lookup.scm in the hw2 subdirectory of your git repository.

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

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:

(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) => ()

Submit the solutions to this exercise in a file named lookup-env.scm in the hw2 subdirectory of your git repository.

Problem 7: Homework Statistics (1 point total)

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.