Brent Yorgey wrote> Someone did respond to your question, with a link to the data-accessor
> package on Hackage (you could also take a look at fclabels or
> lenses). Do those help address your issue?
It's not accessing the data that concerns me, it's updating other elements.
Imagine a system that tracks student registration.
data Student = Student { sName :: String
{- , ... Other stuff -}
, coursesTaking :: [Course]
}
data Rank = Assist | Assoc | Full
data Instructor = Student { iName :: String , rank :: Rank -- defined, say, as data Rank = Assist | Assoc | Full
, coursesTeaching :: [Course]
}
data Course = Course { cName :: String
, units :: Int
, instructor :: Instructor
, enrolledStudents :: [Student]
}
Suppose I want a function that drops a student from a class.
drop :: (Course, Student) ->
I'm declaring the function as pair to pair because both change.
If I run drop, I get a new Course record and a new Student record. Doesn't that mean I have to change all the Student, Instructor, and Course
s that refer to them, which also means I have to change all the Course
s that refer to them, etc.?
Wouldn't I have to write something like this?
data DataBase = DB { students :: [Student]
, instructors :: [Instructor]
, courses :: [Course]
}
drop :: DB -> Course -> Student ->
But even if I do that, doesn't that require a lot of code to get everything updated?
Is there a simple way to write that function?
In a previous message I guessed that the best way to do it is with Maps, e.g. Map Student [Course] etc.
I'd very much appreciate seeing the recommended code for the drop function. Thanks.
-- Russ
P.S. I'll admit that I've spent virtually no time with monads and would prefer to avoid them if possible.