SlideShare uma empresa Scribd logo
1 de 26
Baixar para ler offline
Membership

               Sebastian Rettig


In Haskell normally you understand aa function by reading
 In Haskell normally you understand function by reading
    Function Name ++ Parameter Types + Result Type.
     Function Name Parameter Types + Result Type.
Functional Programming
●   No Variables
●   Functions only, eventually stored in
     Modules
       –   Behavior do not change, once defined
       –   → Function called with same parameter
            calculates always the same result
●   Function definitions (Match Cases)
●   Recursion (Memory)
Haskell Features
●   Pure Functional Programming Language
●   Lazy Evaluation
●   Pattern Matching and Guards
●   List Comprehension
●   Type Polymorphism
Static Type System

●   type of every expression is known at
      compile time
       –   use operation with not compatible types
       –   → program won't compile
       –   → saver code
Nice to remember (1)
●   Types:
       –   starts with uppercase letter
       –   e.g.:
                   ●   Bool
                   ●   Int
                   ●   String
                   ●   [Int]
                   ●   (Bool, Char)
                   ●   Integer
Nice to remember (2)
●   Typevariables
       –   to define generic types
       –   e.g.:
                   ●   maxList :: [a] -> a
                   ●   fst :: (a,b) -> a
                   ●   snd :: (a,b) -> b
       –   Typevariables a and b can contain every
             type (including the same type)
Nice to remember (3)
●   GHCi Commands (Interpreter):
       –   :t ← returns the function header (type)
                      –   :t tail
                              tail :: [a] -> [a]
                      –   :t 2 == 4
                              2 == 4 :: Bool
                      –   :t "HELLO!"
                                "HELLO!" :: [Char]
       –   :i ← returns the function definition (interface)
                      –   :i tail
                              tail :: [a] -> [a]     --
                           Defined in GHC.List
Nice to remember (4)
    Typeclasses:
●   define properties of the types
●   like an interface
         –   Eq can be compared
         –   Ord can be ordered (>, <, >=, <=) (extending Eq)
         –   Show can be shown as string
         –   Read opposite of Show
         –   Enum sequentially ordered types (can be enumerated
             and usable in List-Ranges ['a'..'e'])
Our own Type (1)
●   in the last session, we created the following Type:
    data Shape =
      Circle Float Float Float |
      Rectangle Float Float Float Float
●   and created a function to move a Shape:
    moveLeft :: Shape -> Float -> Shape
    moveLeft (Circle x y r) m = (Circle (x-m) y r)
    moveLeft (Rectangle x1 y1 x2 y2) m =
      (Rectangle (x1-m) y1 (x2-m) y2)
Our own Type (2)
●   → and called it in GHCi and got the following
     Exception:

      Main> moveLeft (Circle 1 2 3) 2
        No instance for (Show Shape)
          arising from a use of `print'
        Possible fix: add an instance declaration
        for (Show Shape)
        In a stmt of an interactive GHCi command:
          print it
Remember : Static Type System
●   type of every expression is known at compile time
        –   use operation with not compatible types
        –   → program won't compile
        –   → saver code
●   QUESTION:
            Would this Code compile?
Remember : Static Type System
●   type of every expression is known at compile time
        –   use operation with not compatible types
        –   → program won't compile
        –   → saver code
●   QUESTION:
            Would this Code compile?

                         YES!
Membership of a Typeclass
●   What happens?
        –   GHCi want's to print out (String) the result
        –   the Typeclass Show converts a type to String
●   → Type must be part of the Typeclass Show
●   two ways to solve this:
        –   inherit from existing implementation of types
              you use
        –   implement the specific typeclass functions by
              yourself
Inherit Membership (1)
●   in the last session, we used the simple way and derived
       the Memberships Show, Eq and Ord from Float Type
       we are using in our Type:
    data Shape =
      Circle Float Float Float |
      Rectangle Float Float Float Float
      deriving (Show, Eq, Ord)
●   and we check our new Typeclass memberships with (:i):
    data Shape = Circle Float Float Float |
      Rectangle Float Float Float Float
         -- Defined at type.hs:3:6-10
      instance Eq Shape -- Defined at type.hs:3:91-92
      instance Ord Shape -- Defined at type.hs:3:95-97
      instance Show Shape -- Defined at type.hs:3:85-88
Inherit Membership (2)
●   and we can now use:
    maxList :: (Ord a) => [a] -> a
    maxList [(Circle 1 2 5), (Circle 2 3 4)]
        –   returns: Circle 2.0 3.0 4.0
●   Ord, Eq & Show implementation of Float works
●   BUT: the result is not correct, why?
        –   Ord, Eq Implementation of Float compares only two
              values
        –   the first value, if equal → second, if equal → third
        –   but we need to compare the third value (radius) only
Implement Membership (1)
●   to solve this, we have to implement Ord and Eq by our own
●   first, we have to find the functions of the typeclass to
        implement :
    :i Eq
      class Eq a where
      (==) :: a -> a -> Bool
      (/=) :: a -> a -> Bool
      -- Defined in GHC.Classes
●   if we look in prelude.hs, we can see the implementation of
        these functions
Implement Membership (2)
●   class keyword defines a new typeclass
    class Eq a where
         (==) :: a -> a -> Bool
         (/=) :: a -> a -> Bool
         x == y = not (x /= y)
         x /= y = not (x == y)
●   a is the type variable
●   we have to implement the membership of Eq for our Type
      Shape
Implement Membership (3)
●   instance keyword defines a membership instance
    instance Eq Shape where
    (Circle _ _ r) == (Circle _ _ r')   =   r == r'
    (Rectangle x1 y1 x2 y2) == (Rectangle x1' y1' x2' y2') =
      (x1 - x2) == (x1' - x2') && (y1 - y2) == (y1' - y2')
    _ == _ = False

●   we don't need to implement (/=), why?
Implement Membership (4)
●   and for the Ord Typeclass:
    :i Ord
      class Eq a => Ord a where
      compare :: a -> a -> Ordering
      (<) :: a -> a -> Bool
      (>=) :: a -> a -> Bool
      (>) :: a -> a -> Bool
      (<=) :: a -> a -> Bool
      max :: a -> a -> a
      min :: a -> a -> a
      -- Defined in GHC.Classes
Implement Membership (5)
    instance Ord Shape where
    a <= b = surface a <= surface b
    a >= b = surface a >= surface b
    a < b = not (a >= b)
    a > b = not (a <= b)

●   min, max and compare just use the implemented
      functions
Type Parameters (1)
●   parameter for a type constructor to create new type
●   e.g.: (Maybe for the World)
             data Maybe a = Nothing | Just a
●   type Maybe is used with another type, but don't care about the
       type
●   e.g.: ghci> :t Just "Hey"
       Just "Hey" :: Maybe [Char]
●   Question: What is the result of:
         –   :t Just 6
         –   :t Nothing
Type Parameters (2)
●   type parameter can also contain typeclass cntraints
●   e.g.:
       data (Ord a) => Maybe a = Nothing | Just a
●   BUT please avoid such design!
         –   → every function must use this constraints
         –   → even if the do not ord anything
●   → better to set constraints in every function header, where
      you need the constraint
         –   max :: (Ord a) => a -> a -> a
Record Syntax
●   for a better structure of your type
    data Car = Car { company :: String
                               , model :: String
                               , year :: String

                               } deriving (Show)
●   instead of:
    data Car = Car String String String
      deriving (Show)
Record Syntax (2)
●   to use in code:
    ghci> Car {company="Ford", model="Mustang",
      year=1967}
●   result:
      Car {company = "Ford", model = "Mustang", year =
      1967}
●   can also contain type parameters:
    data Car a b c = Car { company :: a
                             , model :: b
                             , year :: c
                             } deriving (Show)
Type Synonyms
●   alias for a defined type
●   for better reading and context understanding
●   to define Type Synonym, use the type keyword
        –   type String = [Char]
        –   type Name = String
        –   type Phonenumber = String
        –   type Phonebook = [(Name, Phonenumber)]
●   can also contain parameters
        –   type IntMap v = Map Int v
Sources
[1] Haskell-Tutorial: Learn you a Haskell (http://learnyouahaskell.com/,
    2012/03/15)
[2] The Hugs User-Manual (
    http://cvs.haskell.org/Hugs/pages/hugsman/index.html, 2012/03/15)
[3] The Haskellwiki (http://www.haskell.org/haskellwiki, 2012/03/15)

Mais conteúdo relacionado

Mais procurados

String and string manipulation
String and string manipulationString and string manipulation
String and string manipulation
Shahjahan Samoon
 

Mais procurados (20)

String and string manipulation
String and string manipulationString and string manipulation
String and string manipulation
 
Introduction to ad-3.4, an automatic differentiation library in Haskell
Introduction to ad-3.4, an automatic differentiation library in HaskellIntroduction to ad-3.4, an automatic differentiation library in Haskell
Introduction to ad-3.4, an automatic differentiation library in Haskell
 
From android/java to swift (3)
From android/java to swift (3)From android/java to swift (3)
From android/java to swift (3)
 
Writing DSL with Applicative Functors
Writing DSL with Applicative FunctorsWriting DSL with Applicative Functors
Writing DSL with Applicative Functors
 
Regular Expression
Regular ExpressionRegular Expression
Regular Expression
 
First-Class Patterns
First-Class PatternsFirst-Class Patterns
First-Class Patterns
 
An introduction to functional programming with Swift
An introduction to functional programming with SwiftAn introduction to functional programming with Swift
An introduction to functional programming with Swift
 
Scala jargon cheatsheet
Scala jargon cheatsheetScala jargon cheatsheet
Scala jargon cheatsheet
 
C++11 Idioms @ Silicon Valley Code Camp 2012
C++11 Idioms @ Silicon Valley Code Camp 2012 C++11 Idioms @ Silicon Valley Code Camp 2012
C++11 Idioms @ Silicon Valley Code Camp 2012
 
Typeclasses
TypeclassesTypeclasses
Typeclasses
 
Regexps
RegexpsRegexps
Regexps
 
A Fistful of Functors
A Fistful of FunctorsA Fistful of Functors
A Fistful of Functors
 
Scala cheatsheet
Scala cheatsheetScala cheatsheet
Scala cheatsheet
 
String java
String javaString java
String java
 
Strings in programming tutorial.
Strings  in programming tutorial.Strings  in programming tutorial.
Strings in programming tutorial.
 
09. haskell Context
09. haskell Context09. haskell Context
09. haskell Context
 
OSCON Presentation: Developing High Performance Websites and Modern Apps with...
OSCON Presentation: Developing High Performance Websites and Modern Apps with...OSCON Presentation: Developing High Performance Websites and Modern Apps with...
OSCON Presentation: Developing High Performance Websites and Modern Apps with...
 
Strings IN C
Strings IN CStrings IN C
Strings IN C
 
C# quick ref (bruce 2016)
C# quick ref (bruce 2016)C# quick ref (bruce 2016)
C# quick ref (bruce 2016)
 
Strings v.1.1
Strings v.1.1Strings v.1.1
Strings v.1.1
 

Semelhante a 07. haskell Membership

Fantom - Programming Language for JVM, CLR, and Javascript
Fantom - Programming Language for JVM, CLR, and JavascriptFantom - Programming Language for JVM, CLR, and Javascript
Fantom - Programming Language for JVM, CLR, and Javascript
Kamil Toman
 

Semelhante a 07. haskell Membership (20)

08. haskell Functions
08. haskell Functions08. haskell Functions
08. haskell Functions
 
10. haskell Modules
10. haskell Modules10. haskell Modules
10. haskell Modules
 
Scala qq
Scala qqScala qq
Scala qq
 
01. haskell introduction
01. haskell introduction01. haskell introduction
01. haskell introduction
 
Clojure
ClojureClojure
Clojure
 
Java 8
Java 8Java 8
Java 8
 
Should i Go there
Should i Go thereShould i Go there
Should i Go there
 
Ruby training day1
Ruby training day1Ruby training day1
Ruby training day1
 
Linux shell
Linux shellLinux shell
Linux shell
 
Programming in scala - 1
Programming in scala - 1Programming in scala - 1
Programming in scala - 1
 
02. haskell motivation
02. haskell motivation02. haskell motivation
02. haskell motivation
 
A taste of Functional Programming
A taste of Functional ProgrammingA taste of Functional Programming
A taste of Functional Programming
 
Learning groovy 1: half day workshop
Learning groovy 1: half day workshopLearning groovy 1: half day workshop
Learning groovy 1: half day workshop
 
Scheme 核心概念(一)
Scheme 核心概念(一)Scheme 核心概念(一)
Scheme 核心概念(一)
 
Introduction to ad-3.4, an automatic differentiation library in Haskell
Introduction to ad-3.4, an automatic differentiation library in HaskellIntroduction to ad-3.4, an automatic differentiation library in Haskell
Introduction to ad-3.4, an automatic differentiation library in Haskell
 
Fantom - Programming Language for JVM, CLR, and Javascript
Fantom - Programming Language for JVM, CLR, and JavascriptFantom - Programming Language for JVM, CLR, and Javascript
Fantom - Programming Language for JVM, CLR, and Javascript
 
Dart workshop
Dart workshopDart workshop
Dart workshop
 
Introduction to c
Introduction to cIntroduction to c
Introduction to c
 
Programming with Python - Adv.
Programming with Python - Adv.Programming with Python - Adv.
Programming with Python - Adv.
 
Types, classes and concepts
Types, classes and conceptsTypes, classes and concepts
Types, classes and concepts
 

Último

Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlFuture Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Peter Udo Diehl
 

Último (20)

Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...
Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...
Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...
 
Oauth 2.0 Introduction and Flows with MuleSoft
Oauth 2.0 Introduction and Flows with MuleSoftOauth 2.0 Introduction and Flows with MuleSoft
Oauth 2.0 Introduction and Flows with MuleSoft
 
Connecting the Dots in Product Design at KAYAK
Connecting the Dots in Product Design at KAYAKConnecting the Dots in Product Design at KAYAK
Connecting the Dots in Product Design at KAYAK
 
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlFuture Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
 
Strategic AI Integration in Engineering Teams
Strategic AI Integration in Engineering TeamsStrategic AI Integration in Engineering Teams
Strategic AI Integration in Engineering Teams
 
The Metaverse: Are We There Yet?
The  Metaverse:    Are   We  There  Yet?The  Metaverse:    Are   We  There  Yet?
The Metaverse: Are We There Yet?
 
Speed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in MinutesSpeed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in Minutes
 
AI revolution and Salesforce, Jiří Karpíšek
AI revolution and Salesforce, Jiří KarpíšekAI revolution and Salesforce, Jiří Karpíšek
AI revolution and Salesforce, Jiří Karpíšek
 
Enterprise Knowledge Graphs - Data Summit 2024
Enterprise Knowledge Graphs - Data Summit 2024Enterprise Knowledge Graphs - Data Summit 2024
Enterprise Knowledge Graphs - Data Summit 2024
 
Powerful Start- the Key to Project Success, Barbara Laskowska
Powerful Start- the Key to Project Success, Barbara LaskowskaPowerful Start- the Key to Project Success, Barbara Laskowska
Powerful Start- the Key to Project Success, Barbara Laskowska
 
Syngulon - Selection technology May 2024.pdf
Syngulon - Selection technology May 2024.pdfSyngulon - Selection technology May 2024.pdf
Syngulon - Selection technology May 2024.pdf
 
ECS 2024 Teams Premium - Pretty Secure
ECS 2024   Teams Premium - Pretty SecureECS 2024   Teams Premium - Pretty Secure
ECS 2024 Teams Premium - Pretty Secure
 
Optimizing NoSQL Performance Through Observability
Optimizing NoSQL Performance Through ObservabilityOptimizing NoSQL Performance Through Observability
Optimizing NoSQL Performance Through Observability
 
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptxUnpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
 
Top 10 Symfony Development Companies 2024
Top 10 Symfony Development Companies 2024Top 10 Symfony Development Companies 2024
Top 10 Symfony Development Companies 2024
 
ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...
ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...
ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...
 
WSO2CONMay2024OpenSourceConferenceDebrief.pptx
WSO2CONMay2024OpenSourceConferenceDebrief.pptxWSO2CONMay2024OpenSourceConferenceDebrief.pptx
WSO2CONMay2024OpenSourceConferenceDebrief.pptx
 
Introduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdf
Introduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdfIntroduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdf
Introduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdf
 
IoT Analytics Company Presentation May 2024
IoT Analytics Company Presentation May 2024IoT Analytics Company Presentation May 2024
IoT Analytics Company Presentation May 2024
 
WebAssembly is Key to Better LLM Performance
WebAssembly is Key to Better LLM PerformanceWebAssembly is Key to Better LLM Performance
WebAssembly is Key to Better LLM Performance
 

07. haskell Membership

  • 1. Membership Sebastian Rettig In Haskell normally you understand aa function by reading In Haskell normally you understand function by reading Function Name ++ Parameter Types + Result Type. Function Name Parameter Types + Result Type.
  • 2. Functional Programming ● No Variables ● Functions only, eventually stored in Modules – Behavior do not change, once defined – → Function called with same parameter calculates always the same result ● Function definitions (Match Cases) ● Recursion (Memory)
  • 3. Haskell Features ● Pure Functional Programming Language ● Lazy Evaluation ● Pattern Matching and Guards ● List Comprehension ● Type Polymorphism
  • 4. Static Type System ● type of every expression is known at compile time – use operation with not compatible types – → program won't compile – → saver code
  • 5. Nice to remember (1) ● Types: – starts with uppercase letter – e.g.: ● Bool ● Int ● String ● [Int] ● (Bool, Char) ● Integer
  • 6. Nice to remember (2) ● Typevariables – to define generic types – e.g.: ● maxList :: [a] -> a ● fst :: (a,b) -> a ● snd :: (a,b) -> b – Typevariables a and b can contain every type (including the same type)
  • 7. Nice to remember (3) ● GHCi Commands (Interpreter): – :t ← returns the function header (type) – :t tail tail :: [a] -> [a] – :t 2 == 4 2 == 4 :: Bool – :t "HELLO!" "HELLO!" :: [Char] – :i ← returns the function definition (interface) – :i tail tail :: [a] -> [a] -- Defined in GHC.List
  • 8. Nice to remember (4) Typeclasses: ● define properties of the types ● like an interface – Eq can be compared – Ord can be ordered (>, <, >=, <=) (extending Eq) – Show can be shown as string – Read opposite of Show – Enum sequentially ordered types (can be enumerated and usable in List-Ranges ['a'..'e'])
  • 9. Our own Type (1) ● in the last session, we created the following Type: data Shape = Circle Float Float Float | Rectangle Float Float Float Float ● and created a function to move a Shape: moveLeft :: Shape -> Float -> Shape moveLeft (Circle x y r) m = (Circle (x-m) y r) moveLeft (Rectangle x1 y1 x2 y2) m = (Rectangle (x1-m) y1 (x2-m) y2)
  • 10. Our own Type (2) ● → and called it in GHCi and got the following Exception: Main> moveLeft (Circle 1 2 3) 2 No instance for (Show Shape) arising from a use of `print' Possible fix: add an instance declaration for (Show Shape) In a stmt of an interactive GHCi command: print it
  • 11. Remember : Static Type System ● type of every expression is known at compile time – use operation with not compatible types – → program won't compile – → saver code ● QUESTION: Would this Code compile?
  • 12. Remember : Static Type System ● type of every expression is known at compile time – use operation with not compatible types – → program won't compile – → saver code ● QUESTION: Would this Code compile? YES!
  • 13. Membership of a Typeclass ● What happens? – GHCi want's to print out (String) the result – the Typeclass Show converts a type to String ● → Type must be part of the Typeclass Show ● two ways to solve this: – inherit from existing implementation of types you use – implement the specific typeclass functions by yourself
  • 14. Inherit Membership (1) ● in the last session, we used the simple way and derived the Memberships Show, Eq and Ord from Float Type we are using in our Type: data Shape = Circle Float Float Float | Rectangle Float Float Float Float deriving (Show, Eq, Ord) ● and we check our new Typeclass memberships with (:i): data Shape = Circle Float Float Float | Rectangle Float Float Float Float -- Defined at type.hs:3:6-10 instance Eq Shape -- Defined at type.hs:3:91-92 instance Ord Shape -- Defined at type.hs:3:95-97 instance Show Shape -- Defined at type.hs:3:85-88
  • 15. Inherit Membership (2) ● and we can now use: maxList :: (Ord a) => [a] -> a maxList [(Circle 1 2 5), (Circle 2 3 4)] – returns: Circle 2.0 3.0 4.0 ● Ord, Eq & Show implementation of Float works ● BUT: the result is not correct, why? – Ord, Eq Implementation of Float compares only two values – the first value, if equal → second, if equal → third – but we need to compare the third value (radius) only
  • 16. Implement Membership (1) ● to solve this, we have to implement Ord and Eq by our own ● first, we have to find the functions of the typeclass to implement : :i Eq class Eq a where (==) :: a -> a -> Bool (/=) :: a -> a -> Bool -- Defined in GHC.Classes ● if we look in prelude.hs, we can see the implementation of these functions
  • 17. Implement Membership (2) ● class keyword defines a new typeclass class Eq a where (==) :: a -> a -> Bool (/=) :: a -> a -> Bool x == y = not (x /= y) x /= y = not (x == y) ● a is the type variable ● we have to implement the membership of Eq for our Type Shape
  • 18. Implement Membership (3) ● instance keyword defines a membership instance instance Eq Shape where (Circle _ _ r) == (Circle _ _ r') = r == r' (Rectangle x1 y1 x2 y2) == (Rectangle x1' y1' x2' y2') = (x1 - x2) == (x1' - x2') && (y1 - y2) == (y1' - y2') _ == _ = False ● we don't need to implement (/=), why?
  • 19. Implement Membership (4) ● and for the Ord Typeclass: :i Ord class Eq a => Ord a where compare :: a -> a -> Ordering (<) :: a -> a -> Bool (>=) :: a -> a -> Bool (>) :: a -> a -> Bool (<=) :: a -> a -> Bool max :: a -> a -> a min :: a -> a -> a -- Defined in GHC.Classes
  • 20. Implement Membership (5) instance Ord Shape where a <= b = surface a <= surface b a >= b = surface a >= surface b a < b = not (a >= b) a > b = not (a <= b) ● min, max and compare just use the implemented functions
  • 21. Type Parameters (1) ● parameter for a type constructor to create new type ● e.g.: (Maybe for the World) data Maybe a = Nothing | Just a ● type Maybe is used with another type, but don't care about the type ● e.g.: ghci> :t Just "Hey" Just "Hey" :: Maybe [Char] ● Question: What is the result of: – :t Just 6 – :t Nothing
  • 22. Type Parameters (2) ● type parameter can also contain typeclass cntraints ● e.g.: data (Ord a) => Maybe a = Nothing | Just a ● BUT please avoid such design! – → every function must use this constraints – → even if the do not ord anything ● → better to set constraints in every function header, where you need the constraint – max :: (Ord a) => a -> a -> a
  • 23. Record Syntax ● for a better structure of your type data Car = Car { company :: String , model :: String , year :: String } deriving (Show) ● instead of: data Car = Car String String String deriving (Show)
  • 24. Record Syntax (2) ● to use in code: ghci> Car {company="Ford", model="Mustang", year=1967} ● result: Car {company = "Ford", model = "Mustang", year = 1967} ● can also contain type parameters: data Car a b c = Car { company :: a , model :: b , year :: c } deriving (Show)
  • 25. Type Synonyms ● alias for a defined type ● for better reading and context understanding ● to define Type Synonym, use the type keyword – type String = [Char] – type Name = String – type Phonenumber = String – type Phonebook = [(Name, Phonenumber)] ● can also contain parameters – type IntMap v = Map Int v
  • 26. Sources [1] Haskell-Tutorial: Learn you a Haskell (http://learnyouahaskell.com/, 2012/03/15) [2] The Hugs User-Manual ( http://cvs.haskell.org/Hugs/pages/hugsman/index.html, 2012/03/15) [3] The Haskellwiki (http://www.haskell.org/haskellwiki, 2012/03/15)