November 2021
CSE240 – Assignment 1
Scheme Fundamentals
Topics:
• Understood the concepts of functional programming paradigm.
• Written functional programs in Dr. Racket Scheme.
• Understood names and procedures in functional programming paradigm.
Description
The aim of this assignment is to have you work with Scheme and the Functional
Paradigm.
Use the following Guidelines:
• Give identifiers semantic meaning and make them easy to read (examples
numStudents, grossPay, etc).
• Keep identifiers to a reasonably short length.
• Adopt a coding standard and be consistent. Suggestion:
o Use upper case for constants. Use title case (first letter is upper
case) for classes. Use camelCase for all other identifiers (variables,
methods, objects).
• Use tabs or spaces to indent code within blocks (code surrounded by braces).
This includes classes, methods, and code associated with ifs, switches and
loops. Be consistent with the number of spaces or tabs that you use to
indent.
• Use white space to make your program more readable.
Important Note:
All submitted assignments must begin with the descriptive comment block. To avoid
losing trivial points, make sure this comment header is included in every
assignment you submit, and that it is updated accordingly from assignment to
assignment.
#|
Author:
Date:
Description:
|#
Programming Assignment:
Instructions:
This assignment is going to be made of several functions that you need to create
and often use together to achieve goals. Each function should do one thing well
and should return an appropriate value.
Remember, we should not program side effects in Scheme.
Setup:
1. To complete this assignment, you will need to download and install a copy of
Dr Racket (http://racket-lang.org/download/) on your local PC.
2. Start the program DrRacket.
3. Choose the “R5RS” from the language menu:
Language menu → choose language → Other Languages ➔ R5RS.
Specifications:
Part 1 – Warming up to Scheme with some ridiculous math
Translate the following math statements into a function that returns the value of
that math statement.
Create a function for each called run1, run2, run3, run4 and run5
1) 1 + 2 + 3
2) 3 * ( 2 + 10 / 4 ) + 30
3) 10 – ( ( 3 * 5 ) + ( 2 + ( 0 * 5 ) ) )
4) 5 * ( 4 + ( ( ( 10 + 10 ) + ( 5 * 8 ) ) / ( 10 + 2 ) ) )
5) ( ( ( ( ( ( 3 + 5 ) * ( 6 + 4 ) ) / 2 ) / 2 ) – 5 ) / 3) + ( ( ( ( 2 * 10 )
+ ( 5 * 4 ) ) / 2 ) + ( 4 * 5 ) )
The graders should be able to run each of these as:
(run1)
(run2)
etc.
DO NOT USE (write…)
Part 2 – Visiting Final Fantasy
These functions will take parameters and do calculations.
⌊ ⌋ 𝑚𝑒𝑎𝑛𝑠 𝑓𝑙𝑜𝑜𝑟 (𝑟𝑜𝑢𝑛𝑑 𝑑𝑜𝑤𝑛)
Part A:
Create a function called how-much-damage-ff6? that takes two parameters damage
and defense and calculates how much damage is done based on Final Fantasy 6’s
calculation: ⌊(
𝑑𝑎𝑚𝑎𝑔𝑒∗(255−𝑑𝑒𝑓𝑒𝑛𝑠𝑒)
256
) + 1⌋
(how-much-damage-ff6? 100 100) ➔ 61
Part B:
Final Fantasy 7 has more complex formulas
Create a function called base-attack-ff7 that takes two parameters: strength and
weapon-bonus
Calculation attack = strength + weapon-bonus
(base-attack-ff7 20 18) ➔ 38
Part C:
Create a function called base-damage-ff7 that takes two parameters: level and
base-attack
Calculation: 𝑏𝑎𝑠𝑒_𝑑𝑎𝑚𝑎𝑔𝑒 = 𝑎𝑡𝑡𝑎𝑐𝑘 + ⌊
𝑎𝑡𝑡𝑎𝑐𝑘+𝑙𝑒𝑣𝑒𝑙
32
⌋ ∗ ⌊
𝑎𝑡𝑡𝑎𝑐𝑘∗𝑙𝑒𝑣𝑒𝑙
32
⌋
(base-damage-ff7 6 38) ➔ 45
Part D:
Create a function called calculate-power-ff7 that takes two parameters: powVal
and base-damage
Calculation: 𝑝𝑜𝑤𝑉𝑎𝑙 ∗ (
𝑏𝑎𝑠𝑒𝐷𝑎𝑚𝑎𝑔𝑒
16
)
(calculate-power-ff7 16 45) ➔ 45
Part E:
Create a function called how-much-damage-ff7? that takes two parameters: defense
and power (calculated from previous function)
Calculation: ⌊𝑝𝑜𝑤𝑒𝑟 ∗
512−𝑑𝑒𝑓𝑒𝑛𝑠𝑒
512
⌋
(how-much-damage-ff7? 4 45) ➔ 44
NOTE: Part B – E uses flat numbers for test case purposes, Part F will have you
passing the result from one function into the next.
Part F:
So, to get an attack from start to finish in Final Fantasy 7 we need to:
Write a function get-attack-damage-ff7 that takes in: level, strength, weapon-
bonus, powVal and defense
(get-attack-damage-ff7 6 20 18 16 4) ➔ 44
NOTE: This function calls on all the previous functions you made for FF7.
Calculate
base attack
•base-
attack-ff7
Calculate
base damage
obase-
damage-ff7
Calculate
power
▪calculate-
power-ff7
Calculate
actual
damage
•how-much-
damage-
ff7?
Part 3 – List manipulation
Remember:
• (car ) returns the first item
o (car ‘(1 2 3 4)) ➔ 1
• (cdr ) returns the “back” of the list as a list
o (cdr ‘(1 2 3 4)) ➔ ‘(2 3 4)
Part A:
Using car and cdr, write a function called get-second-item that takes a list as a
parameter and returns the 2
nd
item in the list
(get-second-item ‘(1 2 3 4)) ➔ 2
Part B:
Using car and cdr, write a function called get-third-item that takes a list as a
parameter and returns the 3
rd
item in the list
(get-third-item ‘(1 2 3 4)) ➔ 3
Part C:
Use car and cdr to write a recursive function that returns a naïve count of the
items in the list. Naïve here means we won’t count items contained in sublists,
sublists only count as one item.
This function should be named list-length? and take a list as a parameter.
(list-length? ‘(a b c d)) ➔ 4
(list-length? ‘(a (b c) d)) ➔ 3
HINT: Helper Function Pattern goes a LONG way here.
NOTE: Yes, I know there’s a built in function that does this … you are supposed
to code your own, not call on that.
Part D:
Write a function using car and cdr called arbitrary-cdr that takes a number and a
list as a parameter and returns a ‘cdr’ starting at the item number passed in.
If the number passed in is larger than the list size, return #f
(arbitrary-cdr 3 ‘(a b c d e)) ➔ ‘(c d e)
(arbitrary-cdr 7 ‘(a b c d e)) ➔ #f
HINT: Helper Function Pattern goes a LONG way here.
Part 4 – Sum Number List
Building up the logic
Our goal here is to make a series of functions that will help you solve a
problem. I’m going to walk you through the analysis bit by bit.
Goal:
(sum-number-list ‘(1 2 3 4 5)) ➔ 15
(sum-number-list ‘(1 (2))) → #f
(sum-number-list ‘(a b c)) → #f
Problems:
We are explicitly trying to code a robust, type-safe function to sum a list of
integers.
The function should return the sum of the list if it is a homogenous list of
integers otherwise it should return false.
Problems to solve:
• What is the sum of this list of numbers?
o Is this a homogenous list of numbers?
▪ Is this item a number? – this is solved for us (number? …)
Part A:
Write a function named number-list? that takes a list and returns #t or #f that
it is a list of ONLY numbers.
(number-list? ‘(1 2 3 4)) → #t
(number-list? ‘(1 2 (3) 4) → #f
(number-list? ‘(1 2 a 4) → #f
Part B:
Create the function sum-number-list to make use of your number-list? function and
sum the number list.
Note: you can/should use a helper function to do the work and use sum-number-list
as the interface function that the user would use.
(sum-number-list ‘(1 2 3 4 5)) ➔ 15
(sum-number-list ‘(1 (2))) → #f
(sum-number-list ‘(a b c)) → #f
Basic Test Case:
(display “############# STARTING TEST CASE #############\n”)
(display “************************************”)
(newline)
(display “Testing run functions”)
(newline)
(display “(run1) – answer should be 1 – tested: “)
(run1)
(display “(run2) – answer should be 9 – tested: “)
(run2)
(display “(run3) – answer should be -7 – tested: “)
(run3)
(display “(run4) – answer should be 45 – tested: “)
(run4)
(display “(run5) – answer should be 45 – tested: “)
(run5)
(display “\n############# FINAL FANTASY CASE #############\n”)
(display “Testing FF6 Damamge\n”)
(display “(how-much-damage-ff6? 100 100) – expected 61 – got: “) (how-much-damage-ff6? 100 100)
(display “Testing FF7 Damamge\n”)
(display “(base-attack-ff7 20 18) – expected 38 – got: “) (base-attack-ff7 20 18)
(display “(base-damage-ff7 6 38) – expected 45 – got: “) (base-damage-ff7 6 38)
;;;base damage with function
(display “(base-damage-ff7 6 (base-attack-ff7 20 18)) – expected 45 – got: “) (base-damage-ff7 6
(base-attack-ff7 20 18))
;;;calculate power
(display “(calculate-power-ff7 16 45) – expected 45 – got: “) (calculate-power-ff7 16 45)
;;; how much damage
(display “(how-much-damage-ff7? 4 45) – expected 44 – got: “) (how-much-damage-ff7? 4 45)
(display “(get-attack-damage-ff7 6 20 18 16 4) – expected 44 – got: “) (get-attack-damage-ff7 6 20
18 16 4)
(display “\n############# LIST TEST CASE #############\n”)
(display “(get-second-item ‘(1 2 3 4)) – epxected 2 – got: “)
(get-second-item ‘(1 2 3 4))
(display “(get-third-item ‘(1 2 3 4)) – epxected 3 – got: “)
(get-third-item ‘(1 2 3 4))
(display “(list-length? ‘(1 2 3 4)) – epxected 4 – got: “)
(list-length? ‘(1 2 3 4))
(display “(list-length? ‘(1 2 (1 2 3 4) a (1 2 3 4))) – epxected 5 – got: “)
(list-length? ‘(1 2 (1 2 3 4) a (1 2 3 4)))
(display “arbitrary-cdr 3 ‘(a b c d e)) – epxected ‘(c d e) – got: “)
(arbitrary-cdr 3 ‘(a b c d e))
(display “arbitrary-cdr 7 ‘(a b c d e)) – epxected #f – got: “)
(arbitrary-cdr 7 ‘(a b c d e))
(display “Testing number-list?”)(newline)
(display “(number-list? ‘(1 2 3 4) – epxected #t – got: “)
(number-list? ‘(1 2 3 4))
(display “(number-list? ‘(1 2 (3) 4) – expected #f – got: “)
(number-list? ‘(1 2 (3) 4))
(display “(number-list? ‘(1 2 a 4) – expected #f – got: “)
(number-list? ‘(1 2 a 4))
(newline)
(display “Testing sum-number-list”)(newline)
(display “(sum-number-list ‘(1 2 3 4 5) – expected 15 – got: “)
(sum-number-list ‘(1 2 3 4 5))
(display “(sum-number-list ‘(1 (2))) – expected #f – got: “)
(sum-number-list ‘(1 (2)))
(display “(sum-number-list ‘(a b c)) – expected #f – got: “)
(sum-number-list ‘(a b c))
Grading of Programming Assignment
The TA will grade your program following these steps:
(1) Compile the code. If it does not compile a U or F will be given in the
Specifications section. This will probably also affect the
Efficiency/Stability section.
(2) The TA will read your program and give points based on the points allocated
to each component, the readability of your code (organization of the code and
comments), logic, inclusion of the required functions, and correctness of the
implementations of each function.
Rubric:
What to Submit?
Submit your Doctor Racket file to Canvas
Lastname_Firstname_Hw1.rkt
Please DO NOT zip it for this assignment. I will download all the assignments
and process them to hold a grading test case.
Where to Submit?
All submissions must be electronically submitted to the respected homework link in
the course shell (Canvas) where you downloaded the assignment.
Academic Integrity and Honor Code.
You are encouraged to cooperate in study group on learning the course materials. However, you may not
cooperate on preparing the individual assignments. Anything that you turn in must be your own work: You must
write up your own solution with your own understanding. If you use an idea that is found in a book or from other
sources, or that was developed by someone else or jointly with some group, make sure you acknowledge the
source and/or the names of the persons in the write-up for each problem. When you help your peers, you should
never show your work to them. All assignment questions must be asked in the course discussion board. Asking
assignment questions or making your assignment available in the public websites before the assignment due will
be considered cheating.
The instructor and the TA will CAREFULLY check any possible proliferation or plagiarism. We will use the
document/program comparison tools like MOSS (Measure Of Software Similarity: http://moss.stanford.edu/) to
check any assignment that you submitted for grading. The Ira A. Fulton Schools of Engineering expect all
students to adhere to ASU’s policy on Academic Dishonesty. These policies can be found in the Code of Student
Conduct:
http://www.asu.edu/studentaffairs/studentlife/judicial/academic_integrity.h
tm
ALL cases of cheating or plagiarism will be handed to the Dean’s office. Penalties include a failing grade in the
class, a note on your official transcript that shows you were punished for cheating, suspension, expulsion and
revocation of already awarded degrees.