What is the recommended way to handle complex POD composition (simple data in OO) in Haskell?

I am new to Haskell.

In statically typed OO languages ​​(such as Java), all complex data structures are represented as classes and instances. An object can have many attributes (fields). And another object may be a field value. You can access these fields with your own names and statically enter them by class. Finally, these objects create a huge graph of the object that connects to each other. Most programs use this data graph.

How can I archive these functions in Haskell?

+3
source share
2 answers

, Haskell:

data Person = Person { name    :: String
                     , address :: String }
            deriving (Eq, Read, Show)

data Department = Management | Accounting | IT | Programming
                deriving (Eq, Read, Show)

data Employee = Employee   { identity   :: Person
                           , idNumber   :: Int
                           , department :: Department }
              | Contractor { identity :: Person
                           , company  :: String }
              deriving (Eq, Read, Show)

, a Person Person, name address ( String s); a Department Management, Accounting, IT, Programming; Employee Employee, identity (a Person), a idNumber (an Int) a Department (a Department), Contractor, identity (a Person) a company (a String). deriving (Eq, Read, Show) , .

Haskell ( ) ( ). 1 | (): a Employee Employee, a Contractor, a Department .. :

data Process = Process String Int

, Process ( ) String -> Int -> Process. , , Process "init" 1 Process "ls" 57300. A Process String, Int. , , ; data Person = Person String String,

name :: Person -> String
name (Person n _) = n

address :: Person -> String
address (Person _ a) = a

, , .

, Haskell ; , data Point3 a = Point3 a a a. , Point3 :: a -> a -> a -> Point3 a, Point3 (3 :: Int) (4 :: Int) (5 :: Int), Point3 Int Point3 (1.1 :: Double) (2.2 :: Double) (3.3 :: Double), Point3 Double. ( Point3 1 2 3, Num a => Point3 a, .)

, . : , , , , ( C Python, Prolog Ruby, Erlang Java, ) - . , , , , . , Haskell , , , Java. -: , Java, , (map :: (a -> b) -> [a] -> [b], filter :: (a -> Bool) -> [a] -> [a] foldr :: (a -> b -> b) -> b -> [a] -> b ). . , , . , . : -)


1: : , , data Tree a = Leaf a | Branch a (Tree a) (Tree a).

+5

Source: https://habr.com/ru/post/1791632/


All Articles