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

CSC 3205: Structure and Interpretation of Computer Programs: Exercise 1

This document contains exercises related to the course CSC 3205: Structure and Interpretation of Computer Programs. It includes: 1) Predicting the outcome of Scheme expressions and checking the answers. 2) Writing Scheme expressions for conditional logic related to voting age and grading student marks. 3) Defining procedures for tasks like cubing numbers, finding sums of cubes, temperature conversion, determining leap years, and performing simple calculations. 4) Writing recursive and iterative procedures to count down from a number, double a procedure, and find the sum of integer cubes between 1 and 10.

Uploaded by

Asaba Brian
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

CSC 3205: Structure and Interpretation of Computer Programs: Exercise 1

This document contains exercises related to the course CSC 3205: Structure and Interpretation of Computer Programs. It includes: 1) Predicting the outcome of Scheme expressions and checking the answers. 2) Writing Scheme expressions for conditional logic related to voting age and grading student marks. 3) Defining procedures for tasks like cubing numbers, finding sums of cubes, temperature conversion, determining leap years, and performing simple calculations. 4) Writing recursive and iterative procedures to count down from a number, double a procedure, and find the sum of integer cubes between 1 and 10.

Uploaded by

Asaba Brian
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

CSC 3205: Structure and Interpretation of

Computer Programs

Exercise 1
1. Predict the outcome of the following expressions and check your answer.
a. 75
b. (+ 0 1 2 3 4)
c. (* 4 7)
d. (+ 4 (* 5 1 (/ 20 4)))
e. (define z 9)
f. (define y (+ z 1))
g. (if (< z y)
z
y)
h. (if (< z y)
z)
i. (+ z (if (<= z y)
z
y))
j. (set! z y)
k. (cond
((= z y) (* z y))
((< z y) (- z y))
(else (+ z y)))
l. (case (+ 3 4)
((4) "four")
((5) "five")
((6 7 8) "six or seven or eight")
(else "unknown"))
m. (let
((w 11)
(z 12))
(+ w z))

1
n. v
o. (define (double x) (+ x x))
p. (double 2)
q. (define double2 (lambda (x) (+ x x)))
(double2 2)

2. Write Scheme expressions for the following


a. If a person is above 18 years of age, print a message that allows them
to vote, otherwise print an error message
b. A certain university uses the following criteria for grading students:
if mark is greater than 80, give an A
If mark is between 65 and 80, give a B
If mark is between 50 and 65, give a C
Otherwise, the student has failed.
Write a Scheme expression to grade students.

3. Procedure definition; write procedures for the following tasks: (Note: ex-
tra marks for use of higher-order procedures, let, lambda, conditional ex-
pressions, etc, where appropriate.)
a. A procedure cube that returns the cube of a given number.
b. A procedure sum-of-cubes that returns the sum of cubes of given num-
bers.
c. A procedure to convert a given temperature from
i. Celsius to Fahrenheit
ii. Fahrenheit to Celsius
d. A procedure, leap-year?, that tells whether a given year say 2019 is a
leap year or not.
e. A procedure minimum, which when given 2 values, finds the smallest
value.
f. A procedure maximum, which when given 2 values, finds the biggest
value.
g. A procedure divisible?, which when given 2 values a and b, is able to
tell whether a is divisible by b, otherwise, it returns the remainder.
h. A procedure simple-calc, which takes as arguments a sign(+, -, /, *)
and 2 values, and returns the result of applying the sign to the 2 given
values.
4. Recursion and Iteration. (Note: extra marks will be given for use of
higher-order procedures, let, lambda, conditional expressions, etc, where
appropriate.)

2
a. Write a recursive procedure (count-down n count), that counts down
from a given number n by the value count. For instance (count-down
100 10) counts down from 100 to 0 by reducing by 10.
b. Define a procedure double that takes a procedure of one argument as
argument and returns a procedure that applies the original procedure
twice. For example, if inc is a procedure that adds 1 to its argument,
then (double inc) should be a procedure that adds 2.
c. Write a recursive procedure that returns the sum of cubes of integers
between 1 and 10. Rewrite the same procedure as an iterative process.

You might also like