CS 1321X - Practice Recursion Problems

CS 1321X - Practice Recursion Problems



Here are a number of problems for you to practice with.
Don't just write solutions.  Try to see the patterns and
build on what you already know.

For all the problems, assume they start with "Write a function
in Scheme that ..."

1) Takes a list of numbers as an argument, multiplies all those
values together, and returns the product.

> (multiply-list '(1 2 3 4))
24
> (multiply-list '(0 2 3 4))
0
> (multiply-list '())
1
>

2) Takes a list of numbers as an argument, add those values
together, and returns the sum.

> (sum-list '(1 2 3 4))
10
> (sum-list '(0))
0
> (sum-list '())
0
>

3) Takes a non-empty list of numbers as an argument and 
returns the average of all those numbers.  

> (average '(1 2 3 4 5))
3.0
> (average '(1.5 6.2))
3.85
> (average '(0 0 0))
0
> 

4) Takes a non-empty list of numbers as an argument and
returns the average of all the non-negative values in 
the list.

> (average-non-neg '(-3 2 4 -7 3))
3.0
> (average-non-neg '(-1 -1 -1))
/: division by zero
> (average-non-neg '(1 1 1))
1.0
> 

5) Takes a list of items as an argument and returns the
number of items in that list.

> (count-items '(x y z 1 2 3))
6
> (count-items '(x y (z 1) 2 3))
5
> (count-items '())
0
> 

6) Takes a list of items as an argument and returns the
number of zeros in the list.

> (count-zeros '(x 0 y 2 z 0 3))
2
> (count-zeros '(x y 2))
0
> (count-zeros '())
0
> 

7) Takes a non-empty list of numbers as an argument and 
returns the largest value in the list

> (largest '(3 2 -1 5 6 0 2 7 1))
7
> (largest '(2))
2
> (largest '())
cdr: expects argument of type  given ()
> 

8) Takes a list of boolean values as an argument and
returns the result of AND-ing all those values.

> (my-and '(#f #t #f))
#f
> (my-and '(#t #t))
#t
> (my-and '(#t #f))
#f
> (my-and '(#t))
#t
> (my-and '(#f))
#f
> (my-and '())
#t

9) Takes a list of boolean values as an argument and
returns the result of OR-ing all those values.

> (my-or '(#f #t #f))
#t
> (my-or '(#f #f))
#f
> (my-or '(#t #f))
#t
> (my-or '(#t))
#t
> (my-or '(#f))
#f
> (my-or '())
#f
> 

10) Takes a list of numbers and returns a list of the 
square roots of those numbers.

> (find-sqrts '(1 4 9 16 25))
(1 2 3 4 5)
> (find-sqrts '())
()
> 

11) Takes a list of numbers and returns the list 
that results from adding 3 to each of those numbers.

> (add-3 '(1 2 3 4 5))
(4 5 6 7 8)
> (add-3 '())
()
> 

12) Takes a list of numbers and returns the list
that results from deleting all the zeros.

> (delete-all-zeros '(-1 0 3 0 -2 0 4))
(-1 3 -2 4)
> (delete-all-zeros '())
()
> 

13) Takes a list of numbers and returns a list
of all the non-negative numbers from the original
list.

> (collect-non-neg '(3 -1 0 -4 2 -8))
(3 0 2)
> (collect-non-neg '())
()
> 

14) Takes a list of items and returns the list that
results from reversing or swapping every successive
pair in the original list.  If the original list has
an odd number of items, then the last item is retained
at the end of the new list.

> (swap-every-pair '(1 2 3 4))
(2 1 4 3)
> (swap-every-pair '(1 2 3 4 5))
(2 1 4 3 5)
> 

15) Takes a list of items and returns the list obtained
by repeating every item in the original list.

> (stutter '(a b c))
(a a b b c c)
> (stutter '())
()
> 

16) Takes an non-negative integer n and some item and
creates a list consisting of n of those items.

> (make-list 3 'x)
(x x x)
> (make-list 0 'x)
()
> (make-list 4 '(b))
((b) (b) (b) (b))
> 

17) Takes two lists of items and returns a list obtained
by treating the two original lists as sets and taking the
intersection.  You can assume that if either of the original
lists contains duplicate items, weird results may occur.

> (intersection '(a b c) '(c a e))
(a c)
> (intersection '(a b) '(d))
()
> (intersection '(a b) '(b b))
(b)
> (intersection '(b b) '(a b))
(b b)
> 

18) Takes two lists of items and returns a list obtained
by treating the two original lists as sets and taking the
union.  You can assume that if either of the original
lists contains duplicate items, weird results may occur.

> (union '(a b c) '(c a e))
(b c a e)
> (union '(a b) '(d))
(a b d)
> (union '(a b) '(b b))
(a b b)
> (union '(b b) '(a b))
(a b)
> (union '(a b) '(b a))
(b a)
> 

19) Takes two lists of items and returns a list of lists
obtained by pairing up corresponding items from each 
original list.

> (make-mapping '(a b c) '(1 2 3))
((a 1) (b 2) (c 3))
> (make-mapping '() '())
()
> 

20) Takes two lists of numbers and returns a list 
obtained by adding corresponding values from each
original list.

> (add-pairs '(1 2 3) '(1 2 3))
(2 4 6)
>



Copyright (c) 2003 by Kurt Eiselt.  All rights reserved, with 
the exception of stuff that belongs to somebody else.

Last revised: September 16, 2003