Laboratory Problem 2
Functions and their arguments
In this problem you are asked to build several Scheme functions and analyze the performance of these functions.
Define a procedure subst-1st which takes three arguments: an item new, and item old, and a list of items list. The procedure subst-1st looks for the first occurence of item old in list and replaces it with the item new.
Test your procedure on:
(subst-1st 'dog 'cat '(my cat is clever) ==> (my dog is clever) (subst-1st 'b 'a '(c a b a c)) ==> (c b b a c) (subst-1st '(0) '(*) '((*) (1) (*) (2))) ==> ((0) (1) (*) (2)) (subst-1st 'two 'one '()) ==> ()
In order to be able to include lists as possible arguments to which the parameters new and old are bound, use equal? to test for sameness. Also define procedures substq-1st and substv-1st that use eq? and eqv? respectively instead of equal? to test for sameness.
Finally, analyze the performance of your subst-1st functions. On the average, how many and what kind of operations are performed when applied to a list on n items? Construct a function count which takes a single non-negative integer argument, n, and produces the list of items 1, 2, ..., n. For example,
(count 1) ==> (1) (count 0) ==> () (count 5) ==> (1 2 3 4 5)
Use count as a source of lists to do performance testing of your subst-1st functions.