In this module we introduce the notions of abstraction and accumulators. First we show how recursive functions can be rewritten to maintain information during each recursive call using accumulators. Second we upgrade to the Intermediate Student Language which allows functions as values. We then show how functions can consume/produce other functions allowing for more abstract, and more elegant, designs.
- Given a function that uses mutual recursion, convert it to use accumulators.
- Document accumulator arguments by writing an invariant.
- Write a function definition that is local in scope to another function.
- Given a set of Scheme functions that contain duplicative code, write the abstracted function.
- Given a data definition for tree structured data, write a template.
- Given a specification for information represented as a tree, write a data definition that maps the information into Scheme data.
- Recognize patterns for built-in abstract functions.
- Write the contracts for the built-in abstract functions.
- Given abstract functions, use Higher Order Function Composition to define a new function.
- Given a function that uses structural decomposition, convert it into a function that uses Higher Order Function Composition.
- Given a signature that takes other functions as arguments, write down valid inputs for the signature.
- Given a recursive data definition, write a fold function.
- Recognize patterns for built-in abstract functions.
- Write the contracts for the built-in abstract functions.
- Given abstract functions, use Higher Order Function Composition to define a new function.
- Given a signature that takes other functions as arguments, write down valid inputs for the signature.
- Given a function that uses structural decomposition, convert it into a function that uses Higher Order Function Composition.
- Given a recursive data definition, write a fold function.
- Write an anonymous function using the keyword lambda.
- Write a call to an anonymous function with the appropriate number of inputs.
- Given a Scheme expression, write down the sequence of steps taken during evaluation.
- Use the syntactical conventions for function definitions.
- Given a Scheme function, identify the strategy used.
- Development through Iterative Refinement
- Processing Two Complex Pieces of Data
- Intermezzo 3: Local Definitions and Lexical Scope
- Similarities in Definitions
- Functions are Values
- Designing Abstractions from Examples
- Designing Abstractions from First-Class Functions
- The Loss of Knowledge
- Designing Accumulator-Style Functions
- More Uses of Accumulation
- DUE DATE: Wednesday November 16th at 11:59pm
S-Expressions
You are given the following data definition
;; An S-Expression (SExp) is one of
;; - Atom
;; - LoF<SExp>
;; INTERP: represents an s-expression
;; An Atom is one of
;; - Number
;; - String
;; - Symbol
;; - Boolean
;; INTERP: represents atomic values
-
Provide the deconstructor template for
SExp
-
Design a function that given an
SExp
returns the total number ofAtom
s in theSExp
. -
Design a function that given an
SExp
returns a list of all Symbols in theSExp
. -
Design a function that given an
SExp
returns the sum of all Numbers in theSExp
. -
Design a function that given an
SExp
and two stringss1
ands2
returns a newSExp
where all instance ofs1
have been replaced withs2
.
Higher Order Functions
DrRacket has lots of abstract functions for processing lists (pg. 313, Sect. 21.2, or the online version).
Given the following data definitions:
(define-struct grade (letter num))
;; A Grade is: (make-grade LetterGrade Number)
;; INTERP: represents a homework grade as
;; a letter grade and number grade
;; A LetterGrade is one of:
;; - 'A >= 90
;; - 'B >= 80
;; - 'C >= 70
;; - 'D >= 60
;; - 'F < 60
;; A LoF<Grades>
(define grades (list (make-grade 'D 62) (make-grade 'C 79)
(make-grade 'A 93) (make-grade 'B 84)
(make-grade 'F 57) (make-grade 'F 38)
(make-grade 'A 90) (make-grade 'A 95)
(make-grade 'C 76) (make-grade 'A 90)
(make-grade 'F 55) (make-grade 'C 74)
(make-grade 'A 92) (make-grade 'B 86)
(make-grade 'F 43) (make-grade 'C 73)))
Design the requested functions to manipulate Grades
. You must use the
given list as one of your tests.
For each you may use a local expressions.
Note: if you do not use the DrRacket higher order functions from Section 21.2 in the book, you will not receive credit for the sub-problem!
-
Design the function
log->lon
that converts aLoF<Grade>
into aLoF<Number>
that contains just the numerical grade, using the Scheme functionmap
. -
Using
foldr
, design the functionbest-grade
that finds the highest Grade in aLoF<Grade>
. -
Design a function
just-As
that returns a list of only the 'A grades, usingfilter
. -
Use
andmap
to design the functionall-pass?
that checks to see if all the Grades in a given list are not'F
. -
Finally design the function
bonus
that adds 5 to all of the Grades in a given list, and updates the letter portion of the Grade if it changes. Usemap
to design your function... it must return aLoF<Grade>
!
Trees
A friend of your found this online quiz that they are trying to solve and they are stuck. Help them out by solving these questions.
Provide your solutions without using any abstract functions.
-
Provide a data definition for a Binary Search Tree that contains non-negative Integers (BSTI) and answer the following
questions
- Design a function that given a BSTI returns the sum of all the integers found inside the BSTI.
- Design a function that given a BSTI returns the product of all the integers found inside the BSTI.
-
Design a funciton that given a BSTI and a non negative integer
dx
multiplies each integer in the BSTI bydx
.
-
Provide a data definition for a Binary Search Tree that contains Strings (BSTS). Your BSTS should
use alphabetical ordering to decide which string should be stored to the left or right subtree.
Answer the following questions for a BSTS
- Design a function that given a BSTS appends all the strings in the BSTS using an in-order traversal.
-
Design a function that given a BSTS and a String
ds
appendsds
to each string in the BSTS.
-
Provide a data defintion for a tuple. A tuple always contains 2 values, the first value is a string and the second
value is a non-negative integer. Provide a data definition of a Binary Search Tree that can contain Tuples (BSTT).
Your BSTT should use the alphabetical ordering on the String inside the Tuple's in order to decide which tuple
should be stored to the left or right subtree of a node.
Answer the following questions on BSTT
- Design a function that given a BSTT returns the sum of all Integer values in the Tuples found in the BSTT.
- Design a function that given a BSTT appends all the String values in the Tuples found int he BSTT using an in-order traversal.
- Design a function that given a BSTT returns a BSTS that holds only the String values found in each Tuple in the BSTT.
Now that you have a solution for the 3 different Binary Search Trees we would like to generalize (or abstract) over the data definition and your solution(s).
- Provide a generic data definition for a BST (GBST) that can take any kind of data as an element.
- The solutions to 1.c., 2.b., and 3.c. are similar. Use your existing solutions to design a generic function on GBST. Use your generic function to provide a second solution to 1.c., 2.b., 3.c.
- The solutions to 1.a., 1.b., 2.a., 3.a. and 3.b are similar. Use your existing solutions to design a generic function on GBST. Use your generic function to provide a second solution to 1.a., 1.b., 2.a., 3.a. and 3.b.
- DUE DATE: Wednesday November 23th at 11:59pm
Refactor Space Invaders
- Use your solution from Assignment 6.
-
Update your Assignment 6 to address all comments made by your Grader. All comments means
- comments made that did not cost you points
- comments made that cost you points
-
Review your solution to Assignment 6 and refactor your code such that
- your code uses generic data definitions
-
your code uses the predefined Higher Order Functions provided by DrRacket (e.g.,
map, foldl, filter, etc.
when appropriate. You are also encouraged to develop your own generic functions if that is applicable (i.e., none of the predefined higher order functions can be used) -
your code uses
local
appropriately for helper functions and/or common sub-expressions
Extend Space Invaders
Your space invaders game is popular and you started receiving requests for extensions. Extend your refactored solution to space invaders to accommodate the following extensions.
-
We would like to have a maximum of 3 lives for our spaceship. The game should therefore implement the following
behaviour
- When we start a new game the player has 3 lives. When the spaceship gets hit by an invader bullet, instead the player loses one of his lives. The game ends when the user has 0 lives left.
- The game should display in the bottom right hand corner of its scene the number of remaining lives. When we start a game that number should be the number 3. Upon losing a life the number should be updated accordingly, e.g., from 3 to 2, from 2 to 1 and from 1 to 0. At 0 lives the game is over.
- When an invader bullet hits the spaceship, the spaceship should re-appear in the bottom, center of the screen and moving to the left.
-
We would like to keep score while a user is playing the game. The game should therefore implement the following
behaviour
- The total score should be displayed centered at the top of the window.
- When we start a new game the total score begins with the value of 0. Then during the game for every invader that we hit the score increases by 5. The value displayed on your scene must be updated to reflect the current score.
-
We would like to make the game a little more challenging. We would like the invaders to steadily move from
the top of the screen to the bottom of the screen. The game should therefore implement the following behaviour
- All visible invaders should move down every 10 ticks. Every 10 ticks the invaders should be moved the same amount of units as their height. For example if your invaders have a height of 15 then after 10 ticks all invaders should be moved 15 positions down.
- We need to add a new condition for when the game is over. If any visible invader(s) reach the bottom of the field then the game is over. The bottom of the field is defined to be the top line of your spaceship. For example, if your space ship has a height of 30 units and its center is located at coordinate (10,10), then the bottom of the field is the line y = 25.
-
We would also like to make the game a little more rewarding. We would like to add the ability for the user
to be able to boost their scores and gain lives. The game should therefore implement the following behaviour
- We would like to create some room at the top of the scene before the first row of invaders. This new space should be wide enough to display 1 new invader invader that we will call mother ship.
- The mother ship appears after 30 ticks. The mother ship has the same dimensions as a normal invader but has a different colour than the invaders (and the spaceship).
- The mother shop moves from left to right only and at a constant speed. You are free to select the constant speed that you feel is appropriate.
- If the mother ship is hit by a spaceship bullet then the player gains 20 points and gains 1 life. The game has to add 20 points to the players current score, and, add 1 life to the current total number of lives available to the player.