October 9, 2018

Exercism - Grade School

There are lots of different ways to approach every exercise on Exercism. Why not see how others have solved it?


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?)


There is no much to it this time. I choose a map to represent my database.

(ns grade-school)

(def db {})

(defn grade [school grade]
  (let [stds (get school grade)]
    (if stds

(defn add [school name grade]
  (if (contains? school grade)
    (assoc school grade (conj (get school grade) name))
    (assoc school grade [name])))

(defn sorted [school]
  (let [sorted (sort school)]
    (into {} (map #(hash-map (first %) (sort (second %))) sorted))))

Link to solution at Exercism: Link

Tags: exercism,clojure