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:
Note that all our students only have one name. (It's a small town, what do you want?)
For installation and learning resources, refer to the exercism help page.
To work on the exercises, you will need Opam
and Base
. 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 base:
opam install base
To run the tests you will need OUnit
. Install it using opam
:
opam install ounit
A Makefile is provided with a default target to compile your solution and run the tests. At the command line, type:
make
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.
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!
A pairing session with Phil Battos at gSchool http://gschool.it
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
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)
open Base
(** Grade-school exercise *)
type school = string list Map.M(Int).t
let empty_school =
Map.empty (module Int)
(** Add a student to a school *)
let add name grade school =
let students =
match Map.find school grade with
| Some names -> (name :: names) |> List.sort ~compare:String.compare
| None -> name :: []
in
Map.set school ~key:grade ~data:students
(** Get all the students from a grade *)
let grade g school =
match Map.find school g with
| Some students -> students
| None -> []
(** Sort the list of students in a grade, if necessary *)
let sorted school =
school
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.
Level up your programming skills with 3,104 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors. Exercism is 100% free forever.
Sign up Learn More
Community comments