Tutorial by Examples

Records are an extension of sum algebraic data type that allow fields to be named: data StandardType = StandardType String Int Bool --standard way to create a sum type data RecordType = RecordType { -- the same sum type with record syntax aString :: String , aNumber :: Int , isTrue :...
Suppose you have this type: data Person = Person { name :: String, age:: Int } deriving (Show, Eq) and two values: alex = Person { name = "Alex", age = 21 } jenny = Person { name = "Jenny", age = 36 } a new value of type Person can be created by copying from alex, specif...
Record syntax can be used with newtype with the restriction that there is exactly one constructor with exactly one field. The benefit here is the automatic creation of a function to unwrap the newtype. These fields are often named starting with run for monads, get for monoids, and un for other typ...
{-# LANGUAGE RecordWildCards #-} data Client = Client { firstName :: String , lastName :: String , clientID :: String } deriving (Show) printClientName :: Client -> IO () printClientName Client{..} = do p...
It is possible to define a data type with field labels. data Person = Person { age :: Int, name :: String } This definition differs from a normal record definition as it also defines *record accessors which can be used to access parts of a data type. In this example, two record accessors are de...

Page 1 of 1