How are objects modelled in a functional programming language?
        Posted  
        
            by 
                Giorgio
            
        on Programmers
        
        See other posts from Programmers
        
            or by Giorgio
        
        
        
        Published on 2012-08-13T17:13:02Z
        Indexed on 
            2012/11/25
            11:22 UTC
        
        
        Read the original article
        Hit count: 305
        
object-oriented
|functional-programming
In an answer to this question (written by Pete) there are some considerations about OOP versus FP. In particular, it is suggested that FP languages are not very suitable for modelling (persistent) objects that have an identity and a mutable state.
I was wondering if this is true or, in other words, how one would model objects in a functional programming language. From my basic knowledge of Haskell I thought that one could use monads in some way, but I really do not know enough on this topic to come up with a clear answer.
So, how are entities with an identity and a mutable persistent state normally modelled in a functional language?
EDIT
Here are some further details to clarify what I have in mind. Take a typical Java application in which I can (1) read a record from a database table into a Java object, (2) modify the object in different ways, (3) save the modified object to the database.
How would this be implemented e.g. in Haskell? I would initially read the record into a record value (defined by a data definition), perform different transformations by applying functions to this initial value (each intermediate value is a new, modified copy of the original record) and then write the final record value to the database.
Is this all there is to it? How can I ensure that at each moment in time only one copy of the record is valid / accessible? One does not want to have different immutable values representing different snapshots of the same object to be accessible at the same time.
© Programmers or respective owner