Sunday, 5 June 2016

Haskell: implement course registration system using list comprehensions

Suppose you want to develop an application for an university, where a student can register for a course. To make the things simple, I am going to maintain my data base as list of tuples. Where each tuple looks like below.
(CourseId, StudentId, StudentName)

You need to develp following functions.
a.   Student can register for a course
b.   You can remove a student from registration based on StudentId and courseId
c.    Get all the students register for a course
d.   Get all courseIds registered by a student (Get it by based on StudentId)
e.   Get all student ids and names

Following table summarizes the database.

courseId
studId
studName
C1
1
Hari Krishna
C1
2
Gopi Battu
C1
3
Sudhir Sami
C2
3
Sudhir Sami
C2
2
Gopi Battu
C3
1
Hari Krishna
C3
2
Gopi Battu
C3
3
Sudhir Sami
C3
4
Kiran Darsi

We can represent above data like below.

[("C1", 1, "Hari Krishna"),("C1", 2, "Gopi Battu"),("C1", 3, "Sudhir Sami"),("C2", 3, "Sudhir Sami"),("C2", 2, "Gopi Battu"),("C3", 1, "Hari Krishna"),("C3", 2, "Gopi Battu"),("C3", 3, "Sudhir Sami"),("C3", 4, "Kiran Darsi")]


StudentUtil.hs
module StudentUtil where
    type CourseId = String
    type StudentId = Integer
    type StudentName = String
    type Database = [(CourseId, StudentId, StudentName)]
    type Students = [(StudentId, StudentName)]

    -- Remove student from registration for a course
    removeStudent :: CourseId -> StudentId -> Database -> Database
    removeStudent courseId studId database = [(cId, sId, sName) | (cId, sId, sName) <- database,  not (cId == courseId && sId == studId)]

    -- A student can register for a course
    registerCourse :: CourseId -> StudentId -> StudentName -> Database -> Database
    registerCourse courseId studId studName database = [(courseId, studId, studName)] ++ database

    -- Get all students register for a course
    getStudsRegForCourse :: CourseId -> Database -> Students
    getStudsRegForCourse courseId database = [(sId, sName) | (cId, sId, sName) <- database, (cId == courseId)]

    -- Get all courses registered by a student
    getCoursesRegisteredByStud :: StudentId -> Database -> [CourseId]
    getCoursesRegisteredByStud studId database = [cId | (cId, sId, sName) <- database, (sId == studId)]

    -- Get all students
    getAllStudents :: Database -> Students
    getAllStudents database = [(sId, sName) | (cId, sId, sName) <- database]

*StudentUtil> :load StudentUtil.hs 
[1 of 1] Compiling StudentUtil      ( StudentUtil.hs, interpreted )
Ok, modules loaded: StudentUtil.
*StudentUtil> 
*StudentUtil> let database = [("C1", 1, "Hari Krishna"),("C1", 2, "Gopi Battu"),("C1", 3, "Sudhir Sami"),("C2", 3, "Sudhir Sami"),("C2", 2, "Gopi Battu"),("C3", 1, "Hari Krishna"),("C3", 2, "Gopi Battu"),("C3", 3, "Sudhir Sami"),("C3", 4, "Kiran Darsi")]
*StudentUtil> 
*StudentUtil> database
[("C1",1,"Hari Krishna"),("C1",2,"Gopi Battu"),("C1",3,"Sudhir Sami"),("C2",3,"Sudhir Sami"),("C2",2,"Gopi Battu"),("C3",1,"Hari Krishna"),("C3",2,"Gopi Battu"),("C3",3,"Sudhir Sami"),("C3",4,"Kiran Darsi")]


a. Student can register for a course
*StudentUtil> registerCourse "C1" 5 "Rama Krishna" database
[("C1",5,"Rama Krishna"),("C1",1,"Hari Krishna"),("C1",2,"Gopi Battu"),("C1",3,"Sudhir Sami"),("C2",3,"Sudhir Sami"),("C2",2,"Gopi Battu"),("C3",1,"Hari Krishna"),("C3",2,"Gopi Battu"),("C3",3,"Sudhir Sami"),("C3",4,"Kiran Darsi")]


b. Remove student from registration for a course
*StudentUtil> removeStudent "C1" 1 database
[("C1",2,"Gopi Battu"),("C1",3,"Sudhir Sami"),("C2",3,"Sudhir Sami"),("C2",2,"Gopi Battu"),("C3",1,"Hari Krishna"),("C3",2,"Gopi Battu"),("C3",3,"Sudhir Sami"),("C3",4,"Kiran Darsi")]


c. Get all students register for a course
*StudentUtil> getStudsRegForCourse "C1" database
[(1,"Hari Krishna"),(2,"Gopi Battu"),(3,"Sudhir Sami")]
*StudentUtil> 
*StudentUtil> getStudsRegForCourse "C2" database
[(3,"Sudhir Sami"),(2,"Gopi Battu")]
*StudentUtil> 
*StudentUtil> getStudsRegForCourse "C3" database
[(1,"Hari Krishna"),(2,"Gopi Battu"),(3,"Sudhir Sami"),(4,"Kiran Darsi")]
*StudentUtil> 
*StudentUtil> getStudsRegForCourse "C4" database
[]


d. Get all courses registered by a student

*StudentUtil> getCoursesRegisteredByStud 1 database
["C1","C3"]
*StudentUtil> 
*StudentUtil> getCoursesRegisteredByStud 2 database
["C1","C2","C3"]
*StudentUtil> 
*StudentUtil> getCoursesRegisteredByStud 3 database
["C1","C2","C3"]
*StudentUtil> 
*StudentUtil> getCoursesRegisteredByStud 4 database
["C3"]
*StudentUtil> 
*StudentUtil> getCoursesRegisteredByStud 5 database
[]


e. Get all students

*StudentUtil> getAllStudents database
[(1,"Hari Krishna"),(2,"Gopi Battu"),(3,"Sudhir Sami"),(3,"Sudhir Sami"),(2,"Gopi Battu"),(1,"Hari Krishna"),(2,"Gopi Battu"),(3,"Sudhir Sami"),(4,"Kiran Darsi")]



Previous                                                 Next                                                 Home

No comments:

Post a Comment