CS 270 - Lab 1: Mark W. Boady and Jeremy Johnson Week 1
CS 270 - Lab 1: Mark W. Boady and Jeremy Johnson Week 1
1 Introduction
Assign Roles:
Each member of your group must be assigned a new role. Roles change for each activity period. If you
have 3 people in your group, combine Facilitator and Developer roles.
Facilitator: reads the questions aloud and makes sure everyone contributes. Reports on team members
participation.
Spokesperson: talks to the instructor, TA, and other teams. Only the spokesperson may raise their
hand to ask a question.
Scribe: Records the team’s answers on the Activity Sheet.
Developer: Writes and executes code. Uses computers to calculate answers. The developer is the only
group member that may use a computer during an activity. (This includes phones!)
Facilitator:
Spokesperson:
Scribe:
Developer:
1
CS 270 Activity 1 Week 1
2 Functional Notation
The Racket Programming Language is a purely functional language. It has a syntax that is different from
most programming languages. Each command starts and ends with parenthesis. The function name is always
the first item inside the parenthesis. The function name is followed by any input arguments.
Question 1 : 5 points
Write each of the below expressions in Racket Notation.
(a) (1 point) 7 + 9 * 3
(e) (1 point) 9 * 3 + a * b
Question 2 : 5 points
What does each of the following expressions evaluate to?
(a) (1 point) (+ (* 9 (+ 1 1)) 2)
2
CS 270 Activity 1 Week 1
3 Function Definitions
Racket is designed for recursive loops. Any iteration must be completed recursively. The factorial
function is the product of all numbers between 1 and n.
(
1 n=0
n! =1 ∗ 2 ∗ 3 ∗ 4 · · · ∗ (n − 1) ∗ n = (1)
n ∗ (n − 1)! n>0
(fact 0) 1
(fact 1) 1
(fact 2) 2
(fact 3) 6
Question 3 : 8 points
Answer each of the following questions.
(a) (1 point) What is value is returned by (fact (- 4 1))?
(g) (1 point) How many times does (<= n 1) return false when (fact 6) is executed?
(h) (1 point) How many times does (<= n 1) return true when (fact 6) is executed?
3
CS 270 Activity 1 Week 1
4 Recursive Thinking
Think about exponents. Powers of two are very common in Computer Science. By definition,
20 = 1 (2)
0
x =1 (3)
Question 4 : 6 points
Solve the below to derive a recursive formula for exponents.
(a) (1 point) What is the numerical value of 21 ?
2n
(e) (1 point) What is 2n−1 ?
4
CS 270 Activity 1 Week 1
5 Recursive Summation
Define a Racket function (S n) that sums up all numbers from 0 to n recursively. The below table shows
the first few values for the function.
S(0) 0
S(1) 1
S(2) 3
S(3) 6
S(4) 10
S(5) 15
Question 5 : 6 points
Give your function (S n) in Racket Syntax.