Home Comp 3007
COMP 3007 – Assignment #1
• COMP 3007
◦ Outline
◦ Lectures
◦ Assignments
◦ Schedule
Getting started with Scheme
Due: Sunday September 27th by 11:55:00pm
Objectives: practice with scheme syntax, function definitions, conditionals, and the substitution model.
For each problem you should use the provided file and function names, failure to do so will result in zero marks for the affected solutions.
Question 1
In a file called a1q1_arithmetic.scm, rewrite the following expressions as Scheme expressions:
a. [2 marks] 1 + 2 – 3 + -4 – 5 + 6
b. [2 marks] 20-9+((46/2 + 2)*(20/5 – 1))
c. [2 marks] (123/12+(12/4-14/(13+2))*3)/4
d. [2 marks] ((25*20)*4)+((19/4)+3.15)+((30*2)-47)
Question 2
In a file called a1q2_functions.scm, define the following functions and illustrate their evaluation.
a. [1 mark] Create a procedure (cube x) that computes x3.
b. [2 marks] Create a procedure that computes the following function: f(x) = 2×3 – 4
c.
d. [2 marks] Create a procedure that computes the following function: g(x) = f(2x) – x3
e.
f. [2 marks] Create a procedure that computes the following function: h(x) = 2g(x/2) + f(x)
g.
h. [2 marks] Provide the substitution model using applicative order for (h (* 2 3)) (in comments).
i. [4 marks] Provide the substitution model using normal order for (h (* 2 3)) (in comments).
Question 3
In a file called a1q3_calculations.scm, solve the following problems:
a. [4 marks] Create a function called standard-roundf that takes an input number and a precision value (integer) as arguments and returns the input number rounded to the specified number of decimal places using the “round half up” rounding scheme (see below). Note: if a precision of 0 is specified, your output may include ‘.0’ (ie, still be a floating point number).
E.g. (standard-roundf 1.2224 3) → 1.222
E.g. (standard-roundf 1.2225 3) → 1.223
Note: The built-in round function takes a number and rounds it to the nearest integer using banker’s rounding. In banker’s rounding (a.k.a. “round half to even”), the digit ‘5’ is rounded such that the next digit will be an even number. (e.g. (round 2.5) => 2.0) In the more standard rounding scheme (called “round half up”), the digit ‘5’ is always rounded up. (e.g. (round 2.5) => 3.0) (The two rounding schemes yield the same answer for all other digits).
b. [4 marks] Create a function called area-triangle that takes 3 numbers as input representing the lengths of each side of a triangle, and evaluates to the area of that triangle rounded to at most 3 decimal places.
The area of a triangle can be calculated using Heron’s formula:

where, a, b, and c are the side lengths of the triangle,
and 
Your function should not assume good values are provided for the side lengths (though you may assume the correct types of values). For any instance that would yield an invalid triangle, return an area of 0.
To determine what constitutes valid side lengths of a triangle see, for example: the Triangle Inequality
E.g.: (area-triangle 3 3 5) → 4.146
E.g.: (area-triangle 4 15 6) → 0
E.g.: (area-triangle -2 -5 -2) → 0
Question 4
[10 marks] For this problem you will construct a program over several separate procedures. Your main procedure (see below) should use block structure, and should contain at least two (2) defined inner helper functions. The design of your program should delegate any clear subtasks of this problem to ‘helper’ functions whenever possible.
In a file called a1q4_validate.scm, define a procedure called vali-date? that takes no arguments. The program should prompt the user for a year, a month, and a day (in that order) as integers, and should return true (#t) if the values entered are integers representing a valid date and false (#f) otherwise. You should not assume the inputs are valid nor whether they are of the correct types.
Note: a valid date is one for which the year is a positive integer, the month is a value in the correct range, and the day is a valid day in the given month and year, accounting for leap years. Where a leap year is defined as any year that is a multiple of 4. However, any year that is a multiple of 100 is not a leap year, unless the year is a multiple of 400.
E.g.:
(vali-date?)
Enter a year: 2020
Enter a month: 2
Enter a day: 29
→ #t
(vali-date?)
Enter a year: 2021
Enter a month: 2
Enter a day: 29
→ #f
(vali-date?)
Enter a year: “y2k”
Enter a month: “September”
Enter a day: “15th”
→ #f
For testing, copy/paste the testing runs performed including any user inputs and the result into comments in your submission file.
Question 5
In a file called a1q5_evaluation.txt answer the following questions.
[4 marks] The following program can be used to determine whether a given interpreter is using applicative-order or normal-order evaluation:
(define (x)(x))
(define (test x y)
(if (> x 0)
x
y))
(test 1 (x))
a. [2 marks] What behaviour will be observed with an interpreter that uses applicative-order evaluation? Explain why.
b. [2 marks] What behaviour will be observed with an interpreter that uses normal-order evaluation? Explain why.
Question 6
[3 marks] In a file called a1q6_combinations.txt answer the following question. Observe that Scheme’s model of evaluation allows for combinations where the operator is itself a combination. Use this observation to describe the behaviour of the following procedure:
(define (foo a b)
((cond ((> b 0) +)((= b 0) *)(else /)) a b))
Your answer should describe what happens for all integer values of a and b. Illustrate your answer using the substitution model.
Documentation & Testing
Documentation
• Ensure that your name and student number are in comments at the top of all files.
• Document the purpose of each function including its expected inputs (parameters) and output (return).
• Ensure that your code is well-formatted and easily readable; a happy TA is a generous TA.
• Please note, copying and pasting the assignment guidelines does not constitute sufficient documentation, and will receive zero marks.
• You may use any code or text found in lecture or the assignment but you must cite the source correctly.
Testing
• You are required to include testing runs of every function in your submission.
• The specific tests required depend on the question at hand, but should cover all valid inputs and all possible branches of your code.
• Unless otherwise specified, you may assume inputs supplied are of the correct type.
• Fabricated test outputs will result in 0 marks for a question.
• Your test cases are expected to be unique where appropriate
• Please note, copying and pasting the provided example runs does not constitute sufficient testing, and will receive zero marks.
• For best practices: Comment your testing as to what you are testing and why, giving expected output as well as observed output and explanations for any differences.
[10 marks total]
An example submission including documentation and testing can be found here: primes.scm.
A similar example demonstrating unit testing (borrowed from Racket) can be found here: primes.scm.
You are free to use either approach (or find your own).
Submission Guidelines
• Any files that are not runnable (in DrRacket using R5RS) will result in a mark of 0 for that question.
• You must use any and all provided file and function names for your submission. Failure to follow the prescribed naming conventions will result in a grade of zero for the affected questions.
• Do not use set! (or any built-in procedure with !) in your solutions for this assignment.
• Do not use any external libraries unless otherwise stated. Unsolicited use of imports will result in a mark of zero for any solutions in the given file.
• Combine all files into a single .zip file for your submission.
• Submit your assignment using cuLearn before the due date.
• Marks will be deducted for late submissions.
• You are responsible for submitting all files and ENSURING that the submission is completed successfully.
• If you are having issues with submission, contact me before the due date, afterwards late deductions will apply.
• Please see the course outline for all submission guidelines.
Additional Tips
Some other built-in functions you may find useful (in addition to those covered in the lecture notes):
• (modulo value base),
• (read),
• (log num),
• (floor num),
• (ceiling num),
• (expt num1 num2),
• (number->string num),
• (string-append str1 str2).