0% found this document useful (0 votes)
26 views

Prima Parte

The document contains 7 problems involving Scheme expressions and procedures: 1) Convert arithmetic expressions to Scheme and evaluate them 2) Evaluate Scheme expressions and use car, cdr, cons, quote, list 3) Use car and cdr to return elements from a nested list 4) Define a predicate atom? that checks if an argument is atomic 5) Define a procedure shorter that returns the shorter of two lists 6) Redefine shorter without using length 7) Define a procedure make-list that generates a list of a given length and element

Uploaded by

Vallentyna Bojan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Prima Parte

The document contains 7 problems involving Scheme expressions and procedures: 1) Convert arithmetic expressions to Scheme and evaluate them 2) Evaluate Scheme expressions and use car, cdr, cons, quote, list 3) Use car and cdr to return elements from a nested list 4) Define a predicate atom? that checks if an argument is atomic 5) Define a procedure shorter that returns the shorter of two lists 6) Redefine shorter without using length 7) Define a procedure make-list that generates a list of a given length and element

Uploaded by

Vallentyna Bojan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

1) Convert the following arithmetic expressions into Scheme expressions and evaluate

them.
a. 1.2 (2 - 1/3) + -8.7
b. (2/3 + 4/9) (5/11 - 4/3)
c. 1 + 1 (2 + 1 (1 + 1/2))
d. 1 -2 3 -4 5 -6 7

2) Determine the values of the following expressions. Use your Scheme system to verify
your answers.
a.
(cons 'car 'cdr)
b.
(list 'this '(is silly))
c.
(cons 'is '(this silly?))
d.
(quote (+ 2 3))
e.
(cons '+ '(2 3))
f.
(car '(+ 2 3))
g.
(cdr '(+ 2 3))
h.
cons
i.
(quote cons)
j.
(quote (quote cons))
k.
(car (quote (quote cons)))
l.
(+ 2 3)
m.
(+ '2 '3)
n.
(+ (car '(2 3)) (car (cdr '(2 3))))
o.
((car (list + - * /)) 2 3)

3) (car (car '((a b) (c d)))) yields a. Determine which compositions of car and
cdr applied to ((a b) (c d)) yield b, c, and d.

4) Define the predicate atom?, which returns true if its argument is not a pair and false if
it is.

5) The procedure length returns the length of its argument, which must be a list. For
example, (length '(a b c)) is 3. Using length, define the procedure shorter, which
returns the shorter of two list arguments. Have it return the first list if they have the same
length.
(shorter '(a b) '(c d e)) (a b)
(shorter '(a b) '(c d)) (a b)
(shorter '(a b) '(c)) (c)
6) In exercise 5) you used length in the definition of shorter, which returns the shorter
of its two list arguments, or the first if the two have the same length. Write shorter
without using length.
[Hint: Define a recursive helper, shorter?, and use it in place of the length comparison.]

7) Define the procedure make-list, which takes a nonnegative integer n and an object
and returns a new list, n long, each element of which is the object.
(make-list 7 '()) (() () () () () () ())
[Hint: The base test should be (= n 0), and the recursion step should involve (- n 1).
Whereas () is the natural base case for recursion on lists, 0 is the natural base case for
recursion on nonnegative integers. Similarly, subtracting 1 is the natural way to bring a
nonnegative integer closer to 0.]

You might also like