All of these problems should be done without using side effects (i.e. redefining variables or using set!). Be sure to test your functions on various cases, including empty lists, simple lists with no sublists, complex lists, etc.
(define a '(1 3))
(define b (cons '(4 5) a))
(define c (append a a))
(define a '(1 2 3 4))
(define b '(1 2 3))
(define c (cons a '(a)))
(define e (reverse a))
(define f 33)
(define g 34)show the value that results from evaluating each of the following expressions. If something is wrong, say so. (Obviously you could trivialize the problem by typing these expressions into a Scheme interpreter and transcribing the results. Feel free to use a computer to check your answers, but be sure you can answer the questions without using a machine.)
b
(car e)
(cadr e)
(cons (car b) a)
c
(> 'f 'g)
(list f a)
(equal? (cdr e) b)
(eq? (car b) (car a))
(+ (* f 2) 55 11)
(pair? (car c))
(length c)
Write and test Scheme functions to solve the following problems. You
should save all of your function definitions in a single source file. In
DrScheme you can use File->Save definitions as
to create a file that contains your function definitions.
The normal convention is to use the extension .scm
to end Scheme
file names.
You will be asked to turn in both the function definitions
themselves and a printout showing a Scheme session where you test the
functions. Your test cases should be sufficient to show that the functions
work correctly (i.e., works on both empty and non-empty lists, etc.) In
DrScheme, you can use File->Print Interactions
to print the
session transcript.
(make-pal '(s t e p o n)) => (s t e p o n n o p e t s)
(make-pal '(n e v e r o d)) => (n e v e r o d d o r e v e n)
(even-numbers '(1 2 3 4)) => (2 4)
(even-numbers '(1 17 192 -76)) => (192 -76)
(multiply-numbers '(1 2 3 f 4)) => 24
(multiply-numbers '(14 () (100) 2.5)) => 35.0
(multiply-numbers '(fred 1/2 "says hello" 3/7)) => 3/14
(list-length '(1 2 3 4 () )) => 5
(list-length '((A B) C (D (E F))) => 3
(deep-length '(1 2 3 4 ())) => 4
(deep-length '((A B) C (D (E F))) => 6
For this (as well as most) assignments, you should turn in your work both electronically over the web and on paper.
Turn in a copy of your Scheme source file from part II using this turnin form.
Hand in the following: