Due by class time Tuesday, November 1
The quote mark ' prevents evaluation of a list or symbol:
(+ 2 3) evaluates to the number 5
'(+ 2 3) evaluates to the list (+ 2 3), containing the symbol + and two numbers
'apple evaluates to the symbol apple
The special list () is called the empty list
empty? can be used to check for an empty list:
(empty? '(10 20 30 40)) evaluates to #f, which means false
(empty? '()) evaluates to #t, which means true
first retrieves the first element of a list:
(first '(10 20 30 40)) evaluates to the number 10
rest returns everything but the first element of a list:
(rest '(10 20 30 40)) evaluates to the list (20 30 40)
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) ...)
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) ...)
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) ...)
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) ...)
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) ...)
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) ...)
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) ...)
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) ...)
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) ...)
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) ...)
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) ...)
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) ...)
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) ...)
Save your Scheme definitions in a file called lists.scm (you can choose a different filename if you prefer, but just make sure it ends in .scm) and email it to me as a separate attachment. If you worked with a partner, you should turn in only one file of Scheme definitions for the both of you, but please make sure that both of your names appear in a comment at the beginning of the file, and tell me in your email who your partner is.
If you have questions about anything, don't hesitate to ask!