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 manipulationShahjahan Samoon
 
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 Haskellnebuta
 
From android/java to swift (3)
From android/java to swift (3)From android/java to swift (3)
From android/java to swift (3)allanh0526
 
Writing DSL with Applicative Functors
Writing DSL with Applicative FunctorsWriting DSL with Applicative Functors
Writing DSL with Applicative FunctorsDavid Galichet
 
First-Class Patterns
First-Class PatternsFirst-Class Patterns
First-Class PatternsJohn De Goes
 
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 SwiftFatih Nayebi, Ph.D.
 
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 Sumant Tambe
 
A Fistful of Functors
A Fistful of FunctorsA Fistful of Functors
A Fistful of FunctorsItamar Ravid
 
String java
String javaString java
String java774474
 
Strings in programming tutorial.
Strings  in programming tutorial.Strings  in programming tutorial.
Strings in programming tutorial.Samsil Arefin
 
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...Doris Chen
 
C# quick ref (bruce 2016)
C# quick ref (bruce 2016)C# quick ref (bruce 2016)
C# quick ref (bruce 2016)Bruce Hantover
 

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

Programming in scala - 1
Programming in scala - 1Programming in scala - 1
Programming in scala - 1Mukesh Kumar
 
Learning groovy 1: half day workshop
Learning groovy 1: half day workshopLearning groovy 1: half day workshop
Learning groovy 1: half day workshopAdam Davis
 
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 Haskellnebuta
 
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 JavascriptKamil Toman
 
Introduction to c
Introduction to cIntroduction to c
Introduction to camol_chavan
 
Programming with Python - Adv.
Programming with Python - Adv.Programming with Python - Adv.
Programming with Python - Adv.Mosky Liu
 
Types, classes and concepts
Types, classes and conceptsTypes, classes and concepts
Types, classes and conceptsNicola Bonelli
 

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

[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdfSandro Moreira
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Victor Rentea
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...apidays
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Jeffrey Haguewood
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...apidays
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Cyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdfCyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdfOverkill Security
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024The Digital Insurer
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKJago de Vreede
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfOverkill Security
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024The Digital Insurer
 

Último (20)

[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Cyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdfCyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdf
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 

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)