CS 112: More Lisp Exercises

  1. Write a repeater function that takes a list of lists. Each of the inner lists contains a number and a symbol. The function returns a list of repeating symbols as specified by the inner lists. For example:
    ( repeater '( (3 a) (2 b) ) ) => ( a a a b b )
    ( repeater '( (2 hello) (1 there) (5 x) ) ) => ( hello hello there x x x x x )
    
  2. Write a filter function with two arguments: a number and a list. The function returns a list containing all values in the list argument that are less than or equal to the number. For example:
    ( filter 10 '(20 5 10 25 3 6) ) => ( 5 10 3 6 )
    ( filter 5 '(1 4 9 6 5 6 4 2 1 1 9 8 ) ) => ( 1 4 5 4 2 1 1 )
    
  3. Write a split function with two arguments: a number and a list. The function returns a list of two lists, where the first list in this list contains all numbers that are less than or equal to the number, and the second list contains the rest of the numbers. For example:
    ( split 10 '(20 5 10 25 3 6) ) => ( ( 5 10 3 6 ) ( 20 25 ) )
    ( split 5 '(1 4 9 6 5 6 4 2 1 1 9 8 ) ) => ( ( 1 4 5 4 2 1 1 ) ( 9 6 6 9 8 ) )
    
    Note that the above function is a step towards writing a quick sort function.

  4. This next function is a step towards writing a merge sort function. Write a merge function that takes two lists as arguments and returns a single list. Assume that the arguments are lists of numbers that are already sorted. The list returned is a merged, sorted list. For example:
    ( merge '(1 3 6 8) '(2 4 5 9) ) => ( 1 2 3 4 5 6 8 9 )
    ( merge '(1 2 3 10 20 30 40) '(5 7 9) ) => ( 1 2 3 5 7 9 10 20 30 40 )