[Haskell-beginners] Re: Beginners Digest, Vol 29, Issue 41

Russ Abbott russ.abbott at gmail.com
Fri Nov 26 14:50:54 EST 2010


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

{- ,  ... Other stuff -}
                                    , coursesTeaching :: [Course]
                                    }

data Course = Course { cName :: String
                               , units :: Int

                              {- ,  ... Other stuff -}
                               , instructor :: Instructor
                               , enrolledStudents :: [Student]
                               }

 Suppose I want a function that drops a student from a class.

drop :: (Course,  Student) ->
 (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
 record
s that refer to them, which also means I have to change all the Course
 record
 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 ->
   DB

   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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20101126/d4f9ad42/attachment-0001.html


More information about the Beginners mailing list