COSC 152 Programming Languages
Fall 2006
Homework Assignment #2
Scheme, The Sequel
Due: Friday, Sept 15, 2:00PM CDT
Submit: E-mail electronic
copy to professor, with a subject header "COSC 152 HW#2".
Work is to be completed individually, using only the Scheme constructs
covered in class (TLS Ch 1-5) and the arithmetic primitives (+, -, etc.).
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 '() '())
()
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?
Define a function "flatten" which takes a list (possibly containing
sublists) and returns a new list with all of the atoms at the top
level (i.e. no sublists).
Examples:
> (flatten '((a) ((b c)) (((d) e)) (f)))
(a b c d e f)
Define a function "nth-element" which takes a list and an index 'n',
and returns the S-expression at the n-th location in the list. (The
index should be zero-based, meaning that index '0' refers to the first
item of the list.)
Examples:
> (nth-element '(a b c) 0)
a
> (nth-element '(a b c) 2)
c
> (nth-element '(a b c) 5)
()
Define a function "list-set" which takes a list, an S-expression 's',
and an index 'n', and returns a new list with the n-th item in the
list replaced with s.
Examples:
> (list-set '() 'a 0)
()
> (list-set '(a b c) 'd 2)
(a b d)
Back