Professional Documents
Culture Documents
Exercise 1
(define x 1)
(define (add-x y)
(+ x y))
(define (dbl-x x)
(+ x x))
(define (add-sum y)
(let ((sum (add-x x)))
(+ sum y)))
(define (add-list y)
(every add-x y))
For each of the following expressions, if the expression can be evaluated, write its value.
If the expression cannot be evaluated, explain why not.
1
x
'x
y
'y
(x)
'(x)
('x)
(add-x 1)
(add-x x)
(add-x 'x)
(dbl-x 1)
(dbl-x x)
TCS INTERNAL
(dbl-x 2)
(add-sum 1)
(add-list (1 2 3))
Exercise 2
For each expression, write down its type. Give the most specific type you can: for the
expression 8, number is a better answer than word.
1. x
2. (x)
3. '(x y z)
4. 8
5. + --
6. (+ 9 10)
Exercise 3
For each expression, write down whether it can be evaluated or not (do not write down
the value).
1. (+ 1 2)
2. (+ 1 'two)
3. (+ 1 (2))
4. (word 1 2)
TCS INTERNAL
8. (every (lambda (x) (word 'super- x)) '(duper sonic))
Exercise 4
For all questions, assume the following definition has been executed:
(define wd 'view)
Exercise 5
For all questions, assume the following definitions have been executed:
1. s
2. (car s)
3. (cdr s)
TCS INTERNAL
5. (cons (car s) (cdr s))
7. (cadr s)
9. (car l)
10. (cdr l)
Exercise 6
Then this expression is typed into the Scheme interpreter and evaluated. It returns the
result shown:
The following questions all concern what happens when the expression (rev '(x y z))
is evaluated. It may be helpful to imagine what you would see if you traced the execution
of rev and append.
TCS INTERNAL
1. How many times is rev called?
3. What is the value of (cdr lst) when rev is called the first time?
4. What is the value of (rev (cdr lst)) when rev is called the first time?
5. What is the value of (car lst) when rev is called the first time?
6. What is the value of (list (car lst)) when rev is called the first time?
7. What is the value of (append (rev (cdr lst)) (list (car lst))))) when
rev is called the first time?
Exercise 7
1. 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 a list of sublists, one for each state, where the car of each sublist is the name of
the state and the cdr of each sublist is a list whose only element is the number of
electoral votes for that state.
TCS INTERNAL
Exercise 8
This tree represents the geographic relation of some states, counties and cities in the
USA:
Define a Scheme variable named usa-tree to encode this tree. Represent the tree as a list
whose car is USA and whose cdr is a list of subtrees, one for each of the two states.
Continue filling in subtrees until you reach the cities. At each level, the car of the subtree
holds the data and the cdr of the subtree is a list of subtrees. At each city, the cdr of the
sublist is the empty list to indicate that there are no more subtrees.
Exercise 9
This diagram represents the geographic relation of some cities in the Puget Sound region
of Washington state. A line (possibly a crooked line) connecting two cities means you
can reach one from the other without passing through any of the other cities.
Define a Scheme variable named puget-routes to encode this diagram. Represent city
names by (double-quoted) strings so we can easily represent cities whose names have
more than one word: "Seattle", "Gig Harbor" etc. Represent the diagram as a list of lists.
The car of each list is the name of a city and the cdr of each list is a list of all the cities
that can be reached from the first city.
Exercise 10
Now use your reverse function to write a new function that not only reverses a list, but
reverses each list element in the list as well. Call the function total-reverse. It will
reverse not only the contents of a list but recursively reverse every sublist within the list.
For example
TCS INTERNAL
> (total-reverse (list 1 2 3))
(3 2 1)
> (total-reverse (list 1 (list 2 3)))
((3 2) 1)
> (total-reverse (list 1 (list (list 2 3) 4)))
((4 (3 2)) 1)
Exercise 11
Define a function third to return the third element in a list (e.g., (third '(2 3 4))
should return 4, (third '(4 5 6 7)) should return 6).
Exercise 12
Define a (recursive) function last to return the last value of a list (e.g., (last '(2 3))
should return 3, (last '(4 5 6)) should return 6). You will find the null? function
useful for detecting the end of the list.
Exercise 13
Write the factorial function so that (list-factorial x) will return a list of the
factorials of all the numbers between 1 and x (e.g., (list-factorial 4) should return
(1 2 6 24)).
Exercise 14
Write a Scheme function last-sublist to build a list of the last element in every sublist
in a list. So (last-sublist '((3 2) (4 5)) would return (2 5). Perhaps this function
could use the last function you defined earlier?
Exercise 15
Exercise 16
Exercise 17
Exercise 18
Write a program that reads in three real numbers. Determine computationally which of the following cases
are true: the three numbers
(a) Do not represent a triangle.
(b) Represent an equilateral triangle.
TCS INTERNAL
(c) Represent an isosceles triangle.
(d) Represent a right triangle.
(e) Represent a triangle that is not one of the three immediately above cases.
Exercise 19
Create a structure to define Point.
We want to read a set of (x, y) coordinates and a name, then sort them in order increasing distance from the
origin (0, 0). Use Point.
Exercise 20
Provide a structure definition and a data definition for a paycheck
with check number, name, date, and amount properties. Develop a
function to produce a paycheck given the following information: check
number, name, date, hours worked, wage, and percent tax reported as a
decimal. The amount of the check is calculated by multiplying the hours
worked by the wage less any taxes
Exercise 21
Provide a structure definition and a data definition for an online auction entry, which is
characterized by four pieces of information: the item number, the highest bidder, the
current bid, and 'Open or 'Closed. Develop a function that consumes a bidder, a bid
amount, and an auction entry then returns a new entry. If the bid amount is less than the
current bid or if the auction is closed, then the original entry is returned.
Exercise 22
Develop the function withdrawal, which consumes an account and an amount and
produces a response. In the case in which a withdrawal would cause the balance of a
savings account to go below zero, 'Error would be produced. Otherwise a new savings
account would be produced with a new balance and the number of transactions
incremented by one. A checking account has overdraft protection up to Rs.20000, so a
withdrawal would signal 'Error if the balance were lower than Rs.20000. A credit
account's balance increases upon withdrawal, but cannot go beyond the spending limit.
Exercise 23
Write a program that will accept a fraction of the form N/D, where N is
the numerator and D is the denominator, that prints out the decimal
representation. If the decimal representation has a repeating sequence
of digits, it should be indicated by enclosing it in brackets. For
example, 1/3 = .33333333...is denoted as .(3), and 41/333 =
.123123123...is denoted as .(123).
Exercise 24
TCS INTERNAL
0/1 1/5 1/4 1/3 2/5 1/2 3/5 2/3 3/4 4/5 1/1
Exercise 25
In order to increase security the network manager has introduced some new rules
concerning passwords:
Write a program that inputs a password and then prints REJECTED if the
password is rejected, or ACCEPTED otherwise.
Exercise 26
Write a program that inputs an integer n and ouputs each of the integers from 1 to n
in random order. Each integer should appear once and once only.
Exercise 27
Exercise 28
Write a program that inputs two fractions in the form a/b and c/d, and outputs their
sum in the form p/q cancelled down to its simplest form.
Exercise 29
A sound processor filters out frequencies below a low frequency threshold and above a
high frequency threshold. Such a processor might be used as part of an MP3 encoder to
reduce the amount of data to be processed. Given that a frequency is a number, develop
the function, filter-freq, which consumes two threshold frequencies (low and high) and a
list of frequencies and returns a list of those frequencies between and including the
thresholds.
TCS INTERNAL
Exercise 30
Develop data and structure definitions for trains. A train is one of commuter, which has
the properties: number of cars, number of passengers per car, and a boolean determing
whether this particular train makes all stops; amtrak, which has the properties: number
of cars, number of passengers per car, and a symbol designating the type of train as
'Express, 'Local, or 'Limited; subway, which has the properties: number of cars, number
of passengers per car, and a symbol representing the color of the train.
Develop the function hold-all? that, given a train and a number of passengers, produces
true if the train could contain them all and false if not.
TCS INTERNAL