Avatar of mukeshtiwari

mukeshtiwari's solution

to Grade School in the OCaml Track

Published at Jul 13 2018 · 1 comment
Instructions
Test suite
Solution

Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

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:

  • Start with an empty school.
  • 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?)

Getting Started

For installation and learning resources, refer to the exercism help page.

Installation

To work on the exercises, you will need Opam and Core. Consult opam website for instructions on how to install opam for your OS. Once opam is installed open a terminal window and run the following command to install core:

opam install core

To run the tests you will need OUnit. Install it using opam:

opam install ounit

Running Tests

A Makefile is provided with a default target to compile your solution and run the tests. At the command line, type:

make

Interactive Shell

utop is a command line program which allows you to run Ocaml code interactively. The easiest way to install it is via opam:

opam install utop

Consult utop for more detail.

Feedback, Issues, Pull Requests

The exercism/ocaml repository on GitHub is the home for all of the Ocaml exercises.

If you have feedback about an exercise, or want to help implementing a new one, head over there and create an issue. We'll do our best to help you!

Source

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.

test.ml

open OUnit2
open Grade_school

let assert_lists_equal exp got _ctx =
  let printer = String.concat ";" in
  assert_equal exp got ~printer

let tests = [
  "an empty school has no children in grade 1" >::
    assert_lists_equal [] (grade 1 empty_school);
  "an empty school has Emma in grade 1 after she's been added to grade 1" >::(
    let s = add "Emma" 1 empty_school in
    assert_lists_equal ["Emma"] (grade 1 s)
  );
  "an empty school does not have Emma in grade 1 after she's been added to grade 2" >::(
    let s = add "Emma" 2 empty_school in
    assert_lists_equal [] (grade 1 s)
  );
  "an empty school has Emma, Timmy, Bob and Becky in grade 1 after they've been added to grade 1" >::(
    let s = empty_school |> add "Emma" 1 |> add "Timmy" 1 |> add "Bob" 1 |> add "Becky" 1 in
    assert_lists_equal ["Becky"; "Bob"; "Emma"; "Timmy"] (grade 1 s |> List.sort compare)
  );
  "a sorted school has Emma, Timmy, Bob and Becky in grade 1 in sorted order" >::(
    let s = empty_school |> add "Emma" 1 |> add "Timmy" 1 |> add "Bob" 1 |> add "Becky" 1 |> sorted in
    assert_lists_equal ["Becky"; "Bob"; "Emma"; "Timmy"] (grade 1 s)
  );
]

let () =
  run_test_tt_main ("grade-school tests" >::: tests)
(** Grade-school exercise *)
open Core.Std

type school = string list Int.Map.t


(** Create a new empty school *)
let create : unit -> school =
  fun () -> Int.Map.empty


(** Add a student to a school *)
let add (xs : string)  (n : int)  (mp : school) : school =
  let np = Int.Map.of_alist_exn [(n, [xs])] in
  Int.Map.merge mp np
                ~f:(fun ~key:_ -> function
                     | `Both (u, v) -> Some (u @ v)
                     | `Left u -> Some u
                     | `Right v -> Some v)

(** Get all the students from a grade *)
let grade (n : int)  (mp : school)  :  string list =
  match Int.Map.find mp n with
  | Some v -> v
  | None -> []

(** Sort the list of students in a grade, if necessary *)
let sort (mp : school) :  school =
  Int.Map.to_alist mp
  |> List.map ~f:(fun (a, b) ->
                (a, List.sort ~cmp:(fun x y -> String.compare x y) b))
  |> Int.Map.of_alist_exn



(** Return the list of grades and students as a map. This one
really got my nurves :) *)
let to_map (mp : school) : string list Int.Map.t = mp

Community comments

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

You don't need an anonymous function to define create - there's a more idiomatic way to do it.

I don't think the documentation or type annotations are needed given they are already in the .mli file.

to_map can also be defined to_map = Fn.Id.

Map.add_multi might be useful in the add function.

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?