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 provides an introduction to list processing, functional programming, and Scheme. Implement, document (i.e., write a specification for), and test the following functions in scheme. Scheme includes some of the constructs below, but you may not use these implementations. Instead, you must implement them yourself. 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 in a comment, as we did for the examples in class.

No error-checking is required; you may assume input is valid.

You may not use append for any of the problems below. If you use append for a problem, you will receive a zero for that problem.

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

This assignment is worth 40 points. There are 41 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 hw1 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 hw1 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: (deep-count l) (10 points total)

The function deep-count takes a single argument, l, a list, and returns how many atoms (not including the empty list) are contained in l. An atom is a datum that is not a pair.

Example:

(deep-count '(1 (2 3) (4))) => 4

Here is a quick length function (shallow) that counts the number of elements in list l:

(define (len l)
  (if (null? l)
      0
      (+ 1 (len (cdr l)))))

Here is a quick count function (shallow) that counts the number of occurrences of x in list l:

(define (count x l)
  (if (null? l)
      0
      (if (eq? x (car l))
	  (+ 1 (count x (cdr l)))
	  (count x (cdr l)))))

Problem 2: (reverse l) (10 points total)

Reverse the order of elements in list l. The input is a single list. reverse is “shallow,” i.e., if a list contains a list, then the sublist is unchanged; it simply appears in a different place.

(reverse '(1 (2 3) 4 5)) => (5 4 (2 3) 1)

Hint: To do this in linear time, create a helper function that takes 2 arguments, the list to be reversed, and the reversed list (so far). This is also probably the easier solution since you may not use append (why?).

Problem 3: (range rlist) (10 points total)

The Matlab language supports a convenient notation for specifying ranges of numbers. The notation start:step:end denotes the range of integers , where is the largest integer such that $\textrm{start} + n \times \textrm{step} \le \textrm{end}$ and .

Note that the range may be empty if . Write a scheme function (range (start step end)) which returns the list of integers equal to start:step:end.

This function will take a single rlist, (start step end), as an argument.

Example:

(range '(0 2 7)) => (0 2 4 6)
(range '(2 2 0)) => ()

To make your code a little more legible, use the let construct:

(define (range rlist)
  (let ([start ...]
	[step ...]
	[end ...])
	; your code here, using local variables start, step, end
    ...
    ))

Problem 4: (seq f rlist) (10 points total)

The Maple computer algebra system has a command seq(f, i = m..n, step), which returns the sequence , where is the expression f with all occurrences of the symbol i replaced by the numeric value of i in the sequence of integers from m to n.

Implement a scheme function (seq f (start step end)) which produces a list of values (f(start),f(start+step),...,f(start+n*step)), where n is the largest integer such that and .

Example:

(seq (lambda (x) (* x x)) '(0 2 7)) => (0 4 16 36)

You may not use the built-in map function.

Problem 5: 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.