;;; File: ;;; registrar.ss ;;; Purpose: ;;; Sample utilities for the registrar. ;;; Author: ;;; Samuel A. Rebelsky ;;; Version: ;;; 1.1 of October 2000 ;;; History: ;;; Monday, 23 October 2000 [Version 1.0] ;;; Created. ;;; Tuesday, 24 October 2000 [Version 1.1] ;;; Updated lists of departments. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; General helper procedures ;;; Extract some bit of information about a student. ;;; Parameters: ;;; A student entry in the appropriate format. ;;; Preconditions: ;;; The student entry is in the appropriate format. ;;; Postconditions: ;;; Does not modify the student entry. ;;; Return value: ;;; Some part of the student entry. (define get-student-name (lambda (student) (list-ref student 0))) (define get-student-id (lambda (student) (list-ref student 1))) (define get-student-year (lambda (student) (list-ref student 2))) (define get-student-major (lambda (student) (list-ref student 3))) (define get-student-courses (lambda (student) (list-ref student 4))) ;;; Extract some bit of information about a course. ;;; Parameters: ;;; A course entry in the appropriate format. ;;; Preconditions: ;;; The course entry is in the appropriate format. ;;; Postconditions: ;;; Does not modify the course entry. ;;; Return value: ;;; Some part of the course entry. (define get-course-semester (lambda (course) (list-ref course 0))) (define get-course-department (lambda (course) (list-ref course 1))) (define get-course-number (lambda (course) (list-ref course 2))) (define get-course-grade (lambda (course) (list-ref course 3))) (define get-course-credits (lambda (course) (list-ref course 4))) ;;; Find a student in the database. If the student is not there, ;;; return false. ;;; Parameters: ;;; A name ;;; A list of students ;;; Returns: ;;; The student record for that name, if the name is in the list. ;;; #f, otherwise. ;;; Preconditions: ;;; Each student in the list is in the appropriate format. ;;; Postconditions: ;;; Does not affect the list. (define find-student (lambda (name students) (cond ((null? students) #f) ((equal? name (get-student-name (car students))) (car students)) (else (find-student name (cdr students)))))) ;;; Determine if a string is in a list of strings. ;;; Parameters: ;;; A string. ;;; A potentially empty list of strings. ;;; Preconditions: ;;; None ;;; Postconditions: ;;; Does not affect the list. ;;; Note: ;;; Copied from my own "cd-utils.ss". (define inlist? (lambda (str strlist) ;;; Nothing is in the null list. (if (null? strlist) #f ;;; str is in the list if it's the first element or part ;;; of the remainder.0 (or (equal? str (car strlist)) (inlist? str (cdr strlist)))))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; Exam 1, Problem 8 ;;; ;;; Write a procedure, ;;; (gpa studentname) ;;; that computes a student's GPA. You may wish to create an auxiliary ;;; list that gives a point value for each letter grade. ;;; An association list that gives a numeric value for each letter grade (define grade-values '(("A" . 4.0) ("A-" . 3.67) ("B+" . 3.33) ("B" . 3.0) ("B-" . 2.67) ("C+" . 2.33) ("C" . 2.0) ("D" . 1.0) ("F" . 0.0))) ;;; Compute the numeric value for a grade. ;;; Parameters: ;;; A letter grade ("A", "A-", "B+", "B", "B-", "C+", "C", "D" "F") ;;; Returns: ;;; A numeric value. ;;; Preconditions: ;;; The letter grade must be one of Grinnell's valid letter grades. ;;; Postconditions: ;;; None (define grade-value (lambda (grade) (cdr (assoc grade grade-values)))) ;;; Compute the total number of credits a student received. ;;; Parameters: ;;; A student ;;; Return value: ;;; The number of credits the student received. ;;; Preconditions: ;;; The student is in the appropriate format. ;;; Postconditions: ;;; Does not affect the student. (define credits (lambda (student) (credits-helper (get-student-courses student)))) ;;; Compute the total number of credits for a list of courses. ;;; Parameters: ;;; A list of courses. ;;; Return value: ;;; The total number of credits for those courses. ;;; Preconditions: ;;; Each course is in the appropriate format. (define credits-helper (lambda (courses) (if (null? courses) 0 (+ (get-course-credits (car courses)) (credits-helper (cdr courses)))))) ;;; Compute the total number of points a student received. ;;; Parameters: ;;; A student ;;; Return value: ;;; The number of points the student received. ;;; Preconditions: ;;; The student is in the appropriate format. ;;; Postconditions: ;;; Does not affect the student. (define points (lambda (student) (points-helper (get-student-courses student)))) ;;; Compute the total number of points for a list of courses. ;;; Parameters: ;;; A list of courses. ;;; Return value: ;;; The total number of points for those courses. ;;; Preconditions: ;;; Each course is in the appropriate format. (define points-helper (lambda (courses) (if (null? courses) 0 (+ (* (grade-value (get-course-grade (car courses))) (get-course-credits (car courses))) (points-helper (cdr courses)))))) ;;; Compute a student's GPA. ;;; Parameters: ;;; A student ;;; Return value: ;;; The student's GPA. ;;; Preconditions: ;;; The student is in the appropriate format. ;;; The student has completed at least one course. ;;; Postconditions: ;;; Does not affect the student. (define gpa (lambda (student) (/ (points student) (credits student)))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; Exam 1, Problem 9 ;;; ;;; Write a procedure, ;;; (division-credits division studentname) ;;; that computes the number of credits the student has in a division ;;; (or non-divisional credits, such as tutorial). ;;; For example, ;;; > (division-credits 'Science "Sam Rebelsky") ;;; 54 ;;; Lists of departments in various divisions (define science-departments '("Biology" "Biological Chemistry" "Chemistry" "Math/CS" "Physics" "Psychology" "Science")) (define socsci-departments '("American Studies" "Anthropology" "Economics" "Education" "History" "Political Science" "Sociology" "Social Studies")) (define humanities-departments '("Art" "Art History" "Chinese" "Classics" "English" "French" "German" "Humanities" "Music" "Philosophy" "Religious Studies" "Russian" "Spanish" "Theatre")) (define general-departments '("General" "Tutorial" "Gender and Women's Studies" "Technology Studies" "Indpendent Major")) ;;; Given a department, determine its division ;;; Parameters: ;;; A department [a string] ;;; Return value: ;;; The corresponding division (humanities, science, socsci, general) ;;; [a symbol] ;;; Preconditions: ;;; Must be a valid department ;;; Postconditions: ;;; Does not affect anything. (define department->division (lambda (department) (cond ((inlist? department science-departments) 'science) ((inlist? department socsci-departments) 'socsci) ((inlist? department humanities-departments) 'humanities) (else 'general)))) ;;; How many credits does a student have in a division? ;;; Parameters: ;;; The division (science, socsci, humanities, general) [a symbol] ;;; A student's name [a string] ;;; Returns: ;;; The number of credits the student has in the division. ;;; Preconditions: ;;; The global student database (students) contains the student. ;;; The division is one of the legal values. ;;; Postconditions: ;;; Does not affect the list of students. (define division-credits (lambda (name division) (division-credits-helper (get-student-courses (find-student name students)) division))) ;;; How many credits for a list of courses fall within a division. ;;; Parameters: ;;; A list of courses. ;;; A division. ;;; Preconditions: ;;; The list of courses is in the appropriate format. ;;; The division is valid. (define division-credits-helper (lambda (courses division) (cond ((null? courses) 0) ((equal? division (department->division (get-course-department (car courses)))) (+ (get-course-credits (car courses)) (division-credits-helper (cdr courses) division))) (else (division-credits-helper (cdr courses) division))))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; Exam 1, Problem 10 ;;; ;;; Write a procedure, ;;; (summarize studentname) ;;; that summarizes information about a student. For example, ;;; > (summarize "Sam Rebelsky") ;;; (gpa 3.2 science 54 humanities 20 socsci 30 general 10) ;;; Summarize a student (with a list) ;;; Parameters: ;;; The name of a student. ;;; Return value: ;;; A list that summarizes a student. ;;; Preconditions: ;;; The student must be in the list named "students". ;;; Postconditions: ;;; None (define summarize-student (lambda (name) (summarize-student-helper (find-student name students)))) ;;; Summarize a student (with a list) ;;; Parameters: ;;; A student record. ;;; Return value: ;;; A list that summarizes a student. ;;; Preconditions: ;;; The student must be in the list named "students". ;;; Postconditions: ;;; None (define summarize-student-helper (lambda (student) (list 'gpa (gpa student) 'science (division-credits-helper (get-student-courses student) 'science) 'humanities (division-credits-helper (get-student-courses student) 'humanities) 'socsci (division-credits-helper (get-student-courses student) 'socsci) 'general (division-credits-helper (get-student-courses student) 'general))))