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 1: Scheme functions

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.

This assignment is worth 40 points. There are 41 possible points.

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

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

You may not use append for any of the problems below.

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

A complete submission will include four files:

  1. deep-count.scm
  2. reverse.scm
  3. range.scm
  4. seq.scm

Your code must run on tux under mit-scheme.

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.

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

Submit the solutions to this exercise in a file named deep-count.scm in the hw1 subdirectory of your git repository.

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?).

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

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

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

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.

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

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.