Exercises with Lists and Symbols in Scheme

Due by class time Tuesday, November 1


Non-recursive functions using first, rest, and empty?

  1. Write the function get-first, which takes a list of numbers as input and retrieves the first number from the list, if it exists. If the list is empty, the symbol not-found should be returned instead of an error. Examples:

    (get-first '(1 2 3 4 5)) → 1
    (get-first '(10 20 30)) → 10
    (get-first '(42)) → 42
    (get-first '()) → not-found
    
    (define (get-first nums)
      ...)
    
  2. Write the function get-second, which takes a list of numbers as input and retrieves the second number from the list, if it exists. If the list is empty, or if the list only contains one number, the symbol not-found should be returned instead of an error. Examples:

    (get-second '(1 2 3 4 5)) → 2
    (get-second '(10 20 30)) → 20
    (get-second '(10)) → not-found
    (get-second '()) → not-found
    
    (define (get-second nums)
      ...)
    
  3. Write the function get-third, which takes a list of numbers as input and retrieves the third number from the list, if it exists. If the list contains fewer than three numbers, the symbol not-found should be returned instead of an error. Examples:

    (get-third '(1 2 3 4 5)) → 3
    (get-third '(10 20 30)) → 30
    (get-third '(10 20)) → not-found
    (get-third '(10)) → not-found
    (get-third '()) → not-found
    
    (define (get-third nums)
      ...)
    
  4. Write the function at-least-one?, which takes a list of numbers as input and returns #t (true) if the list contains at least one number, or #f (false) if it doesn't. Examples:

    (at-least-one? '()) → #f
    (at-least-one? '(42)) → #t
    (at-least-one? '(10 20)) → #t
    (at-least-one? '(1 2 3 4 5)) → #t
    
    (define (at-least-one? nums)
      ...)
    
  5. Write the function at-least-two?, which takes a list of numbers as input and returns #t (true) if the list contains at least two numbers, or #f (false) if it doesn't. Examples:

    (at-least-two? '()) → #f
    (at-least-two? '(42)) → #f
    (at-least-two? '(10 20)) → #t
    (at-least-two? '(1 2 3 4 5)) → #t
    
    (define (at-least-two? nums)
      ...)
    


Recursive functions using first, rest, and empty?

  1. Write the recursive function length-of-list, which takes a list as input and returns the total number of list elements. Examples:

    (length-of-list '()) → 0
    (length-of-list '(10 20 30 40)) → 4
    (length-of-list '(42 88 77)) → 3
    (length-of-list '(64)) → 1
    
    (define (length-of-list nums)
       ...)
    
  2. Write the recursive function get-last, which takes a list as input and returns the last (rightmost) element in the list. If the list is empty, the symbol not-found should be returned instead of an error. Examples:

    (get-last '(10 20 30 40)) → 40
    (get-last '(42)) → 42
    (get-last '(42 88 77)) → 77
    (get-last '()) → not-found
    (get-last '(9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 8 7 6 5 44)) → 44
    
    (define (get-last nums)
      ...)
    
  3. Write the recursive function add-all, which takes a list of numbers as input and returns the sum of all of the numbers. If the list contains no numbers at all, the sum is just 0. Examples:

    (add-all '(1 2 3 4)) → 10
    (add-all '(5 2 4 0 3 1)) → 15
    (add-all '(1 3 5 7 9 11)) → 36
    (add-all '(5 5 5 5 5 5 5 5 5 5)) → 50
    (add-all '()) → 0
    
    (define (add-all nums)
      ...)
    
  4. Write the recursive function multiply-all, which takes a list of numbers as input and returns the product of all of the numbers. If the list contains no numbers at all, the product should be 1, not zero. Examples:

    (multiply-all '(1 2 3 4 5)) → 120
    (multiply-all '(2 4 6)) → 48
    (multiply-all '(9)) → 9
    (multiply-all '(2 2 2 2 2 2 2 2 2 2)) → 1024
    (multiply-all '()) → 1
    
    (define (multiply-all nums)
      ...)
    
  5. Write the recursive function count-evens, which takes a list of numbers as input and counts how many even numbers appear in the list. Hint: you can use the built-in Scheme function (even? n) to check if a number n is even. Examples:

    (count-evens '(1 2 3 4 5 6 7)) → 3
    (count-evens '(10 20 30 40)) → 4
    (count-evens '(3 3 3 3 3 3 3 4 3 4 3 4 3 3 3 4 4)) → 5
    (count-evens '(4 3 3 3 3 3 3 4 3 4 3 4 3 3 3 4 4)) → 6
    (count-evens '()) → 0
    
    (define (count-evens nums)
      ...)
    
  6. Write the recursive function count, which takes a number n and a list of numbers nums as input and counts how many times n appears in the list. Examples:

    (count 3 '(1 3 2 3 3 1 1 3 5)) → 4
    (count 3 '()) → 0
    (count 7 '(2 2 2 2 7)) → 1
    (count 4 '(3 3 3 3 3 3 3 4 3 4 3 4 3 3 3 4 4)) → 5
    (count 4 '(4 3 3 3 3 3 3 4 3 4 3 4 3 3 3 4 4)) → 6
    
    (define (count n nums)
      ...)
    
  7. Write the recursive function remove-from-front, which takes a number n and a list of numbers nums as input and returns a new list with the first n elements removed from the front of nums. If nums does not contain at least n elements to begin with, the symbol not-found should be returned instead of an error. Examples:

    (remove-from-front 3 '(10 20 30 40 50)) → (40 50)
    (remove-from-front 2 '(42 88 77)) → (77)
    (remove-from-front 1 '(42 88 77)) → (88 77)
    (remove-from-front 0 '(1 2 3 4)) → (1 2 3 4)
    (remove-from-front 0 '()) → ()
    (remove-from-front 1 '()) → not-found
    (remove-from-front 2 '(99)) → not-found
    
    (define (remove-from-front n nums)
      ...)
    
  8. Write the recursive function get-element, which takes a number n and a list of numbers nums as input and retrieves the nth element from the list. If nums does not contain at least n elements to begin with, the symbol not-found should be returned instead of an error. Examples:

    (get-element 1 '(10 20 30 40 50)) → 10
    (get-element 2 '(10 20 30 40 50)) → 20
    (get-element 4 '(10 20 30 40 50)) → 40
    (get-element 6 '(10 20 30 40 50)) → not-found
    (get-element 2 '(10 20)) → 20
    (get-element 2 '(10)) → not-found
    (get-element 1 '()) → not-found
    
    (define (get-element position nums)
      ...)
    


Turning in Your Homework