The Scheme interpreter evaluates each of these expressions. Write down the value that Scheme returns.
(+ 1 3)
4
(* 3 (+ 1 3))
12
For all parts in this questions, assume the following definitions have been executed:
(define s '(a b c d))
(define t '((a b) c d))
The Scheme interpreter evaluates each of these expressions. Write down the value that Scheme returns.
s
(a b c d)
(car s)
a
(cdr s)
(b c d)
(cons 'a '(b c d))
(a b c d)
(cons (car s) (cdr s))
(a b c d)
(car (cdr s))
b
t
((a b) c d)
(car t)
(a b)
(cdr t)
(c d)
(cons '(a b) '(c d))
((a b) c d)
(list '(a b) '(c d))
((a b) (c d))
(append '(a b) '(c d))
(a b c d)
For all parts in this question, assume that a procedure named
add-1
has been defined. It takes a number as an
argument, and returns the number that is one greater than the
argument. For example (add-1 3)
evaluates to
4
.
The Scheme interpreter evaluates each of these expressions. Write down the value that Scheme returns.
(map add-1 '(1 2 3))
(2 3 4)
(map (lambda (x) (* 2 x)) '(1 2 3))
(2 4 6)
Define a Scheme variable named x
that has the value 3.
(define x 3)
This table shows the number of electoral votes allocated to several states:
Washington | 11 |
Oregon | 7 |
Iowa | 7 |
California | 54 |
Florida | 25 |
Define a Scheme variable named electoral-votes
to encode
this table. Represent the table by an association list: a list of
sublists, one for each state, where each sublist has two elements.
The first element of each sublist is the name of the state and the
second element is the number of electoral votes for that state.
(define electoral-votes '((Washington 11) (Oregon 7) (Iowa 7) (California 54) (Florida 25)))
Define a Scheme procedure named add-1
that takes a number as an argument,
and returns the number that is one greater than the argument.
(define (add-1 x) (+ 1 x))
Define a Scheme procedure named phase
that takes a number
as an argument. If the number is less than zero, it returns the
symbol ice
. If the number is greater than one hundred,
it returns the symbol steam
. Otherwise it returns the
symbol water
.
(define (phase x) (cond ((< x 0) 'ice) ((> x 100) 'steam) (else 'water)))
This definition is executed:
;; Like Scheme length (define (len lst) (if (null? lst) 0 (+ 1 (len (cdr lst)))))
Then this expression is typed into the Scheme interpreter and evaluated. It returns the result shown:
> (len '(x y z)) 3
The following questions all concern what happens when the
expression (len '(x y z))
is evaluated.
len
return when the base case is reached?
len
called?
lst
bound to when len
is called the first time?
(x y z)
(cdr lst)
when len
is called the
first time?
(y z)
(len (cdr lst))
when len
is called the
first time?
(+ 1 (len (cdr lst)))
when len
is called the first time?