COSC 152 Programming Languages

Fall 2006

Homework Assignment #3

Scheme Strikes Back

Due: Friday, Sept 22, 2:00PM CDT
Submit: E-mail electronic copy to professor, with a subject header "COSC 152 HW#3".
Work is to be completed individually, using only the Scheme constructs covered in class (TLS Ch 1-6) and the logical and arithmetic primitives.
Q1 - unique-set
Define a function "unique-set" which takes as input a complex list and returns a simple list containing each item (non-pair) exactly once. Items should appear in the output list in the same order in which they first occured in the input list.
Examples:
> (unique-set '(a b c))
(a b c)
> (unique-set '(a b a a b b c a c b))
(a b c)
> (unique-set '(a ((b a) b) (c)))
(a b c)
Q2 - majority?
Define a predicate "majority*?" which takes a complex list and returns #t only if one of the items comprises more than half of the total items in the list.
Examples:
> (majority*? '(a b c))
#f
> (majority*? '(a b a))
#t
Q3 - mark-depth
Define a function "mark-depth" which takes a complex list and replaces each atom with a two item list containing the atom and an integer indicating the depth in the list. (Top-level items are at depth 0, sub-lists are depth 1, and so on.)
Examples:
> (mark-depth '(a b c))
((a 0) (b 0) (c 0))
> (mark-depth '(a (b (c)) ((d) e f) ((g)) h i))
((a 0) ((b 1) ((c 2))) (((d 2)) (e 1) (f 1)) (((g 2))) (h 0) (i 0))
Q4 - my-map*
Define a function "my-map*" which, without using the built-in primitive "map", takes a function and a complex list as parameters, and returns a list of identical structure but containing the results of applying the function to each item in the list.
Examples:
(my-map* null? '(a b () c))
(#f #f #t #f)
Q5 - every*?
Define a function "every*?" which takes a predicate and a list, and returns #t if and only if the predicate is true for every item in the list.
Examples:
> (every*? number? '(1 2 3 4))
#t
> (every*? null? '(() () (() a) ))
#f

Back