elm/exercises/grade-school/GradeSchool.example

49 lines
1.1 KiB
Text
Raw Normal View History

module GradeSchool (..) where
2015-10-14 19:02:13 +00:00
import Dict exposing (..)
import Result exposing (toMaybe)
2015-10-14 19:02:13 +00:00
-- TODO: implement these classes
-- type Grade = Int
-- type Student = String
-- type School = Dict Int List String
schoolFromList : List ( Int, String ) -> Dict Int (List String)
schoolFromList schoolList =
List.foldl
(\tuple -> \dict -> addStudent (fst tuple) (snd tuple) dict)
newSchool
schoolList
schoolToList : Dict Int (List String) -> List ( Int, List String )
schoolToList school =
Dict.toList school
2015-10-14 19:02:13 +00:00
newSchool : Dict Int (List String)
newSchool =
Dict.empty
2015-10-14 19:02:13 +00:00
addStudent : Int -> String -> Dict Int (List String) -> Dict Int (List String)
addStudent grade student school =
Dict.insert grade (List.sort (student :: (studentsInGrade grade school))) school
2015-10-14 19:02:13 +00:00
gradeWithStudents : Int -> List String -> Dict Int (List String)
gradeWithStudents grade students =
Dict.singleton grade (List.sort students)
2015-10-14 19:02:13 +00:00
studentsInGrade : Int -> Dict Int (List String) -> List String
studentsInGrade grade school =
case (Dict.get grade school) of
Just list ->
list
Nothing ->
[]