On this page:
Student Grades
Students Grades Revisited

Lab 6 List Abstractions

home work!

Purpose This lab focuses on learning to use the built in list functions, map, filter, and foldr. It also gives practice writing code, then going back and adding to it.

As a reminder, here are the signatures for these functions, as well as a simple example using each one.

; map: [X -> Y] [List-of X] -> [List-of Y]
(check-expect (map number->string (list 1 2 3 4))
              (list "1" "2" "3" "4"))
; filter: [X -> Boolean] [List-of X] -> [List-of X]
(check-expect (filter even? (list 1 2 3 4))
              (list 2 4))
; foldr: [X Y -> Y] Y [List-of X] -> Y
(check-expect (foldr * 1 (list 1 2 3 4))
              24)

Since these functions all take a function as an input, you’re going to need to write helper functions as a part of this lab. Sometimes the exercise will tell you to, and sometimes it might not.

None of the exercises in this lab require defining a recursive function. The list functions described above will do the heavy lifting for you.

image

Student Grades

We’re going to model students in a class with a simple structure. The initial goal is to store the overall grade of each student.

; A Student is a (make-student String Number).
; where the name is the student's full name, and the grade is a percentage,
; represented as a number between 0 and 100 inclusive.
(define-struct student (name grade))

Below are some examples that you should use for your tests.

; Employee number ER28-0652.
(define student1 (make-student "Elliot Alderson" 85))
 
; Used to go here.
(define student2 (make-student "Aaron Turon" 97))
 
; Kinda spooky.
(define student3 (make-student "Fox Mulder" 69))
 
; Likes strawberries.
(define student4 (make-student "Kaylee Frye" 82))
 
; This "love" intrigues me. Teach me to fake it.
(define student5 (make-student "Doctor John E. Zoidberg" 58))
 
; students: [List-of Student]
(define students (list student1 student2 student3 student4 student5))

Exercise 1 Design a function extra-credit, that takes a Student, and returns a new Student with some amount of points added. The number of points is up to you; enjoy this power.

Exercise 2 Design the function extra-credits, that takes a list of Students and returns a new list of Students with each Student given extra-credit.

Exercise 3 Design the function student-names, that takes a list of Students and returns a list of Strings. The strings are the names of each student.

Exercise 4 Design the function struggling-students, that takes a list of Students and returns a new list of just the students who are struggling. A struggling student is defined to be anyone with a grade less than 70.

Exercise 5 Design the function sum, that takes a list of Numbers, and returns the sum of the numbers. We’ve done this before, but this time use a list function.

Exercise 6 Design the function average, that takes a list of Numbers, and returns their average (mean).

Exercise 7 Define the function students-average, that takes a list of Students and returns a Number. The number is the average (mean) grade for all the students.

Students Grades Revisited

Now we are going to change the definition of a Student to better track assignments and grades. As you can imagine, storing just the total grade isn’t a very good way to keep a student’s information. Instead we’d really like to know more detail about how a student did on an assignment.

As you have seen, the way assignments get graded is as follows. Start with a total number of points for an assignment, then deduct points for errors. Below are our new data definitions.

If you’d like to have both solutions, feel free to start from a new file, and copy functions from the above exercise as needed.

; A GradedAssignment is a [List-of Number]
; where the first number in the list is the total number of points for the
; assignment, and the rest of the numbers are point deductions from the total.
; Sum up all these numbers to get the actual score on the assignment.
 
; A Student is a (make-student String [List-of GradedAssignment]).
; where the name is the student's full name, and graded-assignments is a list
; of all the assignments for the student. (See Alderson, below, for detailed
; example.)
(define-struct student (name graded-assignments))
 
; Employee number ER28-0652.
(define student1 (make-student "Elliot Alderson"
                               (list (list 100 -8 -2 -4 -6) ; 80/100 = 80%
                                     (list 60 -2 -4)        ; 54/60  = 90%
                                     (list 75 -6 -4))))     ; 65/75  = 86.6%
; Used to go here.
(define student2 (make-student "Aaron Turon"
                               (list (list 100 -1 -4)
                                     (list 60)
                                     (list 75 -1))))
; Kinda spooky.
(define student3 (make-student "Fox Mulder"
                               (list (list 100 -10 -6 -4 -4 -4 -4)
                                     (list 60 -8 -4 -4 -2 -4)
                                     (list 75 -4 -6 -8))))
; Likes strawberries.
(define student4 (make-student "Kaylee Frye"
                               (list (list 100 -4 -8 -2 -4 -4 -1)
                                     (list 60 -4 -1 -2 -2 -1)
                                     (list 75 -4 -4 -1))))
; This "love" intrigues me. Teach me to fake it.
(define student5 (make-student "Doctor John E. Zoidberg"
                               (list (list 100 -10 -4 -10 -8 -6)
                                     (list 60 -10 -8 -10 -2)
                                     (list 75 -4 -10 -4 -8 -4))))
 
; students: [List-of Student]
(define students (list student1 student2 student3 student4 student5))

Exercise 8 We used to have a function student-grade, but no more. Let’s design a replacement using our new data definitions. This function should take a Student and return a Number that is the student’s average grade, on a [0,100] scale, across all his graded assignments.

You may need to design a few helper functions to help with this exercise.

Exercise 9 Update all the functions from the first section of this lab to work correctly with the new data definitions. Remember to update not just the code, but the signature, purpose statement, and tests too.