1. Using Scheme as a desk calculator, determine the arithmetic mean of the numbers 74, 81, 87, and 90.
(/ (+ 74 81 87 90) 4)2. Give a Scheme expression of which the value is a four-element list with the number 7 as its first element, the symbol
a
the
second, the number -12 the third, and the symbol ???
the
fourth.
'(7 a -12 ???)or
(cons 7 (cons (quote a) (cons -12 (cons (quote ???) (quote ())))))3. Give a Scheme expression of which the value is a three-element list with the symbol
*
as its first element and the number
-5
as its second and third.
'(* -5 -5)or
(cons (quote *) (cons -5 (cons -5 (quote ()))))4. Give a Scheme expression that, when evaluated, determines whether the first element of the three-element list mentioned in the previous exercise is a symbol. (The result of the evaluation should be
#t
.)
(symbol? (car '(* -5 -5)))5. Give a Scheme expression of which the value is a two-element list with the empty list as its first and second element.
'(() ())or
(cons (quote ()) (cons (quote ()) (quote ())))6. After the definition
(define dozen 12)what will be the value of the Scheme expression
(number?
dozen)
?
#t7. Give a Scheme definition that will determine the sum of 31, 29, 31, 30, 31, and 10 and bind the identifier
day-of-year
to this
sum.
(define day-of-year (+ 31 29 31 30 31 10))8. Define a Scheme procedure
square
that, given any number
as an argument, returns the square of that number.
(define square (lambda (root) (* root root)))9. Define a recursive Scheme procedure
first-symbol
that
finds and returns the first element of a given list that is a symbol (but
returns #f
if none of the elements of the list is a
symbol).
(define first-symbol (lambda (li) (cond ((null? li) #f) ((symbol? (car li)) (car li)) (else (first-symbol (cdr li))))))10. Define a recursive Scheme procedure
add-1-to-each
that,
given any list of numbers, returns a list of equal length in which each
element is 1 greater than the corresponding element of the given list. For
example, the value of (add-1-to-each '(3 8 6))
should be
(4 9 5)
.
(define add-1-to-each (lambda (li) (if (null? li) '() (cons (+ (car li) 1) (add-1-to-each (cdr li))))))11. Define a recursive Scheme procedure
tally
that counts and
returns the number of occurrences of a given value in a given list. For
example, the value of (tally 'a '(b a 7 c a a 3 a))
should be
4
.
(define tally (lambda (val li) (cond ((null? li) 0) ((equal? (car li) val) (+ 1 (tally val (cdr li)))) (else (tally val (cdr li))))))12. Define a Scheme procedure
all-different?
that determines
whether all of the top-level elements of a given list are distinct (that
is, not equal?
).
This solution uses the member?
procedure defined in the
textbook (program 2.3, page 50).
(define all-different? (lambda (li) (if (null? li) #t (and (not (member? (car li) (cdr li))) (all-different? (cdr li))))))