COSC 3410 Programming Languages

Fall 2011

Homework Assignment #1

A Gentle Introduction to Scheme
Due: Wednesday, Sept 07, 11:00am CDT
Submit: Turn in a single Scheme source file called "hw1.scm" using the turnin command on the Systems Lab machines. Include the names of all authors at the top of the file in a comment block. Work is to be completed individually, using only the Scheme constructs covered in class (TLS Ch 1-4) and the arithmetic primitives (+, -, etc.).

Q1 - append-item

Define a function "append-item" which takes an S-expression and a list, and returns a new list with the S-expression appended at the end. Do not use the built-in append.
Examples:
> (append-item 'a '())
(a)
> (append-item 'e '(a b c d))
(a b c d e)

Q2 - append-list

Define a function "append-list" which takes two lists and returns a new list with the contents of the first list followed by the contents of the second.
Examples:
> (append-list '(a b) '(c d))
(a b c d)
> (append-list '() '())
()

Q3 - rotate-list

Define a function "rotate-list" which takes a list a returns a new list with the first S-expression now at the end of the list.
Examples:
> (rotate-list '(a b c d e))
(b c d e a)

Q4 - rotate-list-n

Define a function "rotate-list-n" which takes a list and an integer n, and returns a new list which has been rotated n times.
Examples:
> (rotate-list-n '(a b c d e) 2)
(c d e a b)

Q5 - reverse-list

Define a function "reverse-list" which takes a list and returns a new list with the S-expressions in reverse order. Do not use the built-in reverse.
Examples:
> (reverse-list '(a b c d e))
(e d c b a)

Q6 - up and down

Define two functions "up-level" and "down-level", each of which takes a list a returns a new list with the contents moved up or down a level of list depth. Examples:
> (down-level '(a b c))
((a) (b) (c))
> (up-level '((a) (b) (c)))
(a b c)
> (down-level '(a (b c) ((d) e) f))
((a) ((b c)) (((d) e)) (f))
> (up-level '((a) ((b c)) (((d) e)) (f)))
(a (b c) ((d) e) f)
Also, answer the following questions in comments in your code:
  • When is (up-level (down-level x)) the same as x?
  • When is (down-level (up-level x)) the same as x?

  • Back
    [Revised 2011 Aug 29 11:41 DWB]