Avatar of rabuf

rabuf's solution

to Grade School in the Common Lisp Track

Published at Jan 20 2020 · 0 comments
Test suite

Given students' names along with the grade that they are in, create a roster for the school.

In the end, you should be able to:

  • Add a student's name to the roster for a grade
    • "Add Jim to grade 2."
    • "OK."
  • Get a list of all students enrolled in a grade
    • "Which students are in grade 2?"
    • "We've only got Jim just now."
  • Get a sorted list of all students in all grades. Grades should sort as 1, 2, 3, etc., and students within a grade should be sorted alphabetically by name.
    • "Who all is enrolled in school right now?"
    • "Grade 1: Anna, Barb, and Charlie. Grade 2: Alex, Peter, and Zoe. Grade 3…"

Note that all our students only have one name. (It's a small town, what do you want?)

For bonus points

Did you get the tests passing and the code clean? If you want to, these are some additional things you could try:

  • If you're working in a language with mutable data structures and your implementation allows outside code to mutate the school's internal DB directly, see if you can prevent this. Feel free to introduce additional tests.

Then please share your thoughts in a comment on the submission. Did this experiment make the code better? Worse? Did you learn anything from it?


Check out Installing Common Lisp for instructions to get started or take a look at the guides available in the track's side bar.


While Common Lisp doesn't care about indentation and layout of code, nor whether you use spaces or tabs, this is an important consideration for submissions to exercism.io. Excercism.io's code widget cannot handle mixing of tab and space characters well so using only spaces is recommended to make the code more readable to the human reviewers. Please review your editors settings on how to accomplish this. Below are instructions for popular editors for Common Lisp.


Use the following commands to ensure VIM uses only spaces for indentation:

:set tabstop=2
:set shiftwidth=2
:set expandtab

(or as a oneliner :set tabstop=2 shiftwidth=2 expandtab). This can be added to your ~/.vimrc file to use it all the time.


Emacs is very well suited for editing Common Lisp and has many powerful add-on packages available. The only thing that one needs to do with a stock emacs to make it work well with exercism.io is to evaluate the following code:

(setq-default indent-tabs-mode nil)

This can be placed in your ~/.emacs (or ~/.emacs.d/init.el) in order to have it set whenever Emacs is launched.

One suggested add-on for Emacs and Common Lisp is SLIME which offers tight integration with the REPL; making iterative coding and testing very easy.


A pairing session with Phil Battos at gSchool http://gschool.it

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.


(ql:quickload "lisp-unit")
#-xlisp-test (load "grade-school")

(defpackage #:grade-school-test
  (:use #:common-lisp #:lisp-unit))

(in-package #:grade-school-test)

(define-test grade-roster-is-initially-empty
  (let ((school (school:make-school)))
    (assert-equalp '() (school:grade-roster school))))

(define-test add-student
  (let ((school (school:make-school)))
    (school:add school "Aimee" 2)
    (assert-equalp '((:grade 2 :students ("Aimee")))
                   (school:grade-roster school))))

(define-test add-more-students-in-same-class
  (let ((school (school:make-school)))
    (school:add school "James" 2)
    (school:add school "Blair" 2)
    (school:add school "Paul" 2)
    (let ((grade2 (first (school:grade-roster school))))
      (assert-equalp 2 (getf grade2 :grade))
      (assert-equalp '("Blair" "James" "Paul")
                     (sort (getf grade2 :students) #'string<)))))

(define-test add-students-to-different-grades
  (let ((school (school:make-school)))
    (school:add school "Chelsea" 3)
    (school:add school "Logan" 7)
    (assert-equalp '((:grade 3 :students ("Chelsea"))
                     (:grade 7 :students ("Logan")))
                   (sort (school:grade-roster school) #'< :key #'second))))

(define-test get-students-in-a-grade
  (let ((school (school:make-school)))
    (school:add school "Franklin" 5)
    (school:add school "Bradley" 5)
    (school:add school "Jeff" 1)
    (assert-equalp '("Bradley" "Franklin")
                   (sort (school:grade school 5) #'string<))))

(define-test get-students-in-a-non-existant-grade
  (let ((school (school:make-school)))
    (assert-equalp '() (school:grade school 1))))

(define-test sorted-school
  (let ((school (school:make-school)))
    (school:add school "Jennifer" 4)
    (school:add school "Kareem" 6)
    (school:add school "Christopher" 4)
    (school:add school "Kyle" 3)
    (assert-equalp '((:grade 3 :students ("Kyle"))
                     (:grade 4 :students ("Christopher" "Jennifer"))
                     (:grade 6 :students ("Kareem")))
                   (school:sorted school))))

(let ((*print-errors* t)
      (*print-failures* t))
  (run-tests :all :grade-school-test))
(defpackage #:school
  (:use #:common-lisp)
  (:export #:make-school #:add #:grade-roster #:grade #:sorted))

(in-package #:school)

(defclass school ()
  ((students :accessor students
             :initarg :students
             :initform (make-hash-table))))

(defun make-school ()
  (make-instance 'school))
(defgeneric add (school student grade))
(defmethod add ((school school) student grade)
  (setf (gethash grade (students school)) (cons student (gethash grade (students school) nil))))
(defgeneric grade-roster (school))
(defmethod grade-roster ((school school))
  (loop for grade being the hash-keys in (students school) using (hash-value students)
     collect (list :grade grade :students students)))
(defgeneric grade (school grade))
(defmethod grade ((school school) grade)
  (gethash grade (students school) nil))
(defgeneric sorted (school))
(defmethod sorted ((school school))
  (sort (grade-roster school) #'< :key #'cadr))

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?