More Exercises with Lists and Symbols in Scheme


Recursive functions using cons and list

  1. Write the recursive function laugh, which takes a number n and constructs a list of n ha symbols. Hint: use the cons function. Examples:

    (laugh 0) → ()
    (laugh 3) → (ha ha ha)
    (laugh 4) → (ha ha ha ha)
    (laugh 10) → (ha ha ha ha ha ha ha ha ha ha)
    
    (define (laugh n)
      ...)
    
  2. Write the recursive function copies, which takes a number n and any symbol as input, and constructs a list of n input symbols. This function is similar to laugh, except it uses whatever symbol is provided to it. Examples:

    (copies 5 'yes) → (yes yes yes yes yes)
    (copies 4 'no) → (no no no no)
    (copies 0 'yes) → ()
    (copies 3 'hee) → (hee hee hee)
    (copies 4 'hee) → (hee hee hee hee)
    (copies 10 'ha) → (ha ha ha ha ha ha ha ha ha ha)
    
    (define (copies n x)
      ...)
    
  3. Write the recursive function double-all, which takes a list of numbers as input, and constructs a new list with all of the numbers doubled. Examples:

    (double-all '(1 3 5 7 9)) → (2 6 10 14 18)
    (double-all '(3 5 7 9)) → (6 10 14 18)
    (double-all '()) → ()
    (double-all '(10 10 10)) → (20 20 20)
    (double-all '(5 3 4 9 1 6 5 7)) → (10 6 8 18 2 12 10 14)
    
    (define (double-all nums)
      ...)
    
  4. Write the recursive function add-to-end, which takes a number n and a list of numbers as input, and constructs a new list with n added onto the end of the input list. Hint: notice that if the input list is empty, the answer should not be n itself, but a list containing the single number n. Examples:

    (add-to-end 10 '(1 2 3 4 5)) → (1 2 3 4 5 10)
    (add-to-end 10 '(2 3 4 5)) → (2 3 4 5 10)
    (add-to-end 10 '()) → (10)
    (add-to-end 9 '(5 5 5 3 3 3 1 1 1)) → (5 5 5 3 3 3 1 1 1 9)
    
    (define (add-to-end n nums)
      ...)
    
  5. Write the recursive function remove-last, which takes a list of numbers as input, and constructs a new list with the last (rightmost) number removed. If the list is empty to begin with, the result should be the symbol not-found. Examples:

    (remove-last '(1 2 3 4 5)) → (1 2 3 4)
    (remove-last '(2 3 4 5)) → (2 3 4)
    (remove-last '(5)) → ()
    (remove-last '()) → not-found
    (remove-last '(5 5 5 3 3 3 1 1 1 9)) → (5 5 5 3 3 3 1 1 1)
    
    (define (remove-last nums)
      ...)
    
  6. Write the recursive function range, which takes two numbers start and end as input, and constructs a new list containing all numbers in sequence from start up to and including end. However, if end is greater than start to begin with, the empty list should be returned, since there are no numbers in that range. Examples:

    (range 1 8) → (1 2 3 4 5 6 7 8)
    (range 2 8) → (2 3 4 5 6 7 8)
    (range 8 8) → (8)
    (range 9 8) → ()
    (range 50 60) → (50 51 52 53 54 55 56 57 58 59 60)
    
    (define (range start end)
      ...)
    
  7. Write the recursive function expand-GOD, which takes a number n as input, and expands the acronym GOD n times, as shown below. If n is 0, just the symbol GOD should be returned. Otherwise, the appropriate (possibly nested) list should be constructed. Examples:

    (expand-GOD 0) → GOD
    (expand-GOD 1) → (GOD Over Djinn)
    (expand-GOD 2) → ((GOD Over Djinn) Over Djinn)
    (expand-GOD 3) → (((GOD Over Djinn) Over Djinn) Over Djinn)
    (expand-GOD 5) → (((((GOD Over Djinn) Over Djinn) Over Djinn) Over Djinn) Over Djinn)
    
    (define (expand-GOD n)
      ...)