SlideShare a Scribd company logo
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)
Ad

More Related Content

What's hot (20)

String and string manipulation
String and string manipulationString and string manipulation
String and string manipulation
Shahjahan 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 Haskell
nebuta
 
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 Functors
David Galichet
 
Regular Expression
Regular ExpressionRegular Expression
Regular Expression
Mahzad Zahedi
 
First-Class Patterns
First-Class PatternsFirst-Class Patterns
First-Class Patterns
John 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 Swift
Fatih Nayebi, Ph.D.
 
Scala jargon cheatsheet
Scala jargon cheatsheetScala jargon cheatsheet
Scala jargon cheatsheet
Ruslan Shevchenko
 
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
 
Typeclasses
TypeclassesTypeclasses
Typeclasses
ekalyoncu
 
Regexps
RegexpsRegexps
Regexps
Eugene Nizhibitsky
 
A Fistful of Functors
A Fistful of FunctorsA Fistful of Functors
A Fistful of Functors
Itamar Ravid
 
Scala cheatsheet
Scala cheatsheetScala cheatsheet
Scala cheatsheet
Arduino Aficionado
 
String java
String javaString java
String java
774474
 
Strings in programming tutorial.
Strings  in programming tutorial.Strings  in programming tutorial.
Strings in programming tutorial.
Samsil Arefin
 
09. haskell Context
09. haskell Context09. haskell Context
09. haskell Context
Sebastian Rettig
 
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
 
Strings IN C
Strings IN CStrings IN C
Strings IN C
yndaravind
 
C# quick ref (bruce 2016)
C# quick ref (bruce 2016)C# quick ref (bruce 2016)
C# quick ref (bruce 2016)
Bruce Hantover
 
Strings v.1.1
Strings v.1.1Strings v.1.1
Strings v.1.1
BG Java EE Course
 
String and string manipulation
String and string manipulationString and string manipulation
String and string manipulation
Shahjahan 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 Haskell
nebuta
 
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 Functors
David Galichet
 
First-Class Patterns
First-Class PatternsFirst-Class Patterns
First-Class Patterns
John 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 Swift
Fatih 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 Functors
Itamar Ravid
 
String java
String javaString java
String java
774474
 
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
 

Similar to 07. haskell Membership (20)

08. haskell Functions
08. haskell Functions08. haskell Functions
08. haskell Functions
Sebastian Rettig
 
10. haskell Modules
10. haskell Modules10. haskell Modules
10. haskell Modules
Sebastian Rettig
 
Scala qq
Scala qqScala qq
Scala qq
羽祈 張
 
01. haskell introduction
01. haskell introduction01. haskell introduction
01. haskell introduction
Sebastian Rettig
 
Clojure
ClojureClojure
Clojure
Rohit Vaidya
 
Principles of programming language intro
Principles of programming language introPrinciples of programming language intro
Principles of programming language intro
RajeswariA8
 
Java 8
Java 8Java 8
Java 8
vilniusjug
 
Should i Go there
Should i Go thereShould i Go there
Should i Go there
Shimi Bandiel
 
Ruby training day1
Ruby training day1Ruby training day1
Ruby training day1
Bindesh Vijayan
 
Linux shell
Linux shellLinux shell
Linux shell
Kenny (netman)
 
Programming in scala - 1
Programming in scala - 1Programming in scala - 1
Programming in scala - 1
Mukesh Kumar
 
02. haskell motivation
02. haskell motivation02. haskell motivation
02. haskell motivation
Sebastian Rettig
 
A taste of Functional Programming
A taste of Functional ProgrammingA taste of Functional Programming
A taste of Functional Programming
Jordan Open Source Association
 
Learning groovy 1: half day workshop
Learning groovy 1: half day workshopLearning groovy 1: half day workshop
Learning groovy 1: half day workshop
Adam Davis
 
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
nebuta
 
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
 
Dart workshop
Dart workshopDart workshop
Dart workshop
Vishnu Suresh
 
Introduction to c
Introduction to cIntroduction to c
Introduction to c
amol_chavan
 
Programming with Python - Adv.
Programming with Python - Adv.Programming with Python - Adv.
Programming with Python - Adv.
Mosky Liu
 
Principles of programming language intro
Principles of programming language introPrinciples of programming language intro
Principles of programming language intro
RajeswariA8
 
Programming in scala - 1
Programming in scala - 1Programming in scala - 1
Programming in scala - 1
Mukesh Kumar
 
Learning groovy 1: half day workshop
Learning groovy 1: half day workshopLearning groovy 1: half day workshop
Learning groovy 1: half day workshop
Adam 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 Haskell
nebuta
 
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
 
Introduction to c
Introduction to cIntroduction to c
Introduction to c
amol_chavan
 
Programming with Python - Adv.
Programming with Python - Adv.Programming with Python - Adv.
Programming with Python - Adv.
Mosky Liu
 
Ad

Recently uploaded (20)

Dev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath MaestroDev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
UiPathCommunity
 
How Can I use the AI Hype in my Business Context?
How Can I use the AI Hype in my Business Context?How Can I use the AI Hype in my Business Context?
How Can I use the AI Hype in my Business Context?
Daniel Lehner
 
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdfSAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
Precisely
 
tecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdftecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdf
fjgm517
 
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc
 
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
BookNet Canada
 
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In France
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In FranceManifest Pre-Seed Update | A Humanoid OEM Deeptech In France
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In France
chb3
 
Big Data Analytics Quick Research Guide by Arthur Morgan
Big Data Analytics Quick Research Guide by Arthur MorganBig Data Analytics Quick Research Guide by Arthur Morgan
Big Data Analytics Quick Research Guide by Arthur Morgan
Arthur Morgan
 
Electronic_Mail_Attacks-1-35.pdf by xploit
Electronic_Mail_Attacks-1-35.pdf by xploitElectronic_Mail_Attacks-1-35.pdf by xploit
Electronic_Mail_Attacks-1-35.pdf by xploit
niftliyevhuseyn
 
Generative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in BusinessGenerative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in Business
Dr. Tathagat Varma
 
How analogue intelligence complements AI
How analogue intelligence complements AIHow analogue intelligence complements AI
How analogue intelligence complements AI
Paul Rowe
 
Linux Professional Institute LPIC-1 Exam.pdf
Linux Professional Institute LPIC-1 Exam.pdfLinux Professional Institute LPIC-1 Exam.pdf
Linux Professional Institute LPIC-1 Exam.pdf
RHCSA Guru
 
Role of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered ManufacturingRole of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered Manufacturing
Andrew Leo
 
ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes Partner Innovation Updates for May 2025ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes
 
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep DiveDesigning Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
ScyllaDB
 
Cybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure ADCybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure AD
VICTOR MAESTRE RAMIREZ
 
What is Model Context Protocol(MCP) - The new technology for communication bw...
What is Model Context Protocol(MCP) - The new technology for communication bw...What is Model Context Protocol(MCP) - The new technology for communication bw...
What is Model Context Protocol(MCP) - The new technology for communication bw...
Vishnu Singh Chundawat
 
Cyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of securityCyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of security
riccardosl1
 
AI and Data Privacy in 2025: Global Trends
AI and Data Privacy in 2025: Global TrendsAI and Data Privacy in 2025: Global Trends
AI and Data Privacy in 2025: Global Trends
InData Labs
 
Procurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptxProcurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptx
Jon Hansen
 
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath MaestroDev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
UiPathCommunity
 
How Can I use the AI Hype in my Business Context?
How Can I use the AI Hype in my Business Context?How Can I use the AI Hype in my Business Context?
How Can I use the AI Hype in my Business Context?
Daniel Lehner
 
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdfSAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
Precisely
 
tecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdftecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdf
fjgm517
 
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc
 
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
BookNet Canada
 
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In France
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In FranceManifest Pre-Seed Update | A Humanoid OEM Deeptech In France
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In France
chb3
 
Big Data Analytics Quick Research Guide by Arthur Morgan
Big Data Analytics Quick Research Guide by Arthur MorganBig Data Analytics Quick Research Guide by Arthur Morgan
Big Data Analytics Quick Research Guide by Arthur Morgan
Arthur Morgan
 
Electronic_Mail_Attacks-1-35.pdf by xploit
Electronic_Mail_Attacks-1-35.pdf by xploitElectronic_Mail_Attacks-1-35.pdf by xploit
Electronic_Mail_Attacks-1-35.pdf by xploit
niftliyevhuseyn
 
Generative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in BusinessGenerative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in Business
Dr. Tathagat Varma
 
How analogue intelligence complements AI
How analogue intelligence complements AIHow analogue intelligence complements AI
How analogue intelligence complements AI
Paul Rowe
 
Linux Professional Institute LPIC-1 Exam.pdf
Linux Professional Institute LPIC-1 Exam.pdfLinux Professional Institute LPIC-1 Exam.pdf
Linux Professional Institute LPIC-1 Exam.pdf
RHCSA Guru
 
Role of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered ManufacturingRole of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered Manufacturing
Andrew Leo
 
ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes Partner Innovation Updates for May 2025ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes
 
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep DiveDesigning Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
ScyllaDB
 
Cybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure ADCybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure AD
VICTOR MAESTRE RAMIREZ
 
What is Model Context Protocol(MCP) - The new technology for communication bw...
What is Model Context Protocol(MCP) - The new technology for communication bw...What is Model Context Protocol(MCP) - The new technology for communication bw...
What is Model Context Protocol(MCP) - The new technology for communication bw...
Vishnu Singh Chundawat
 
Cyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of securityCyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of security
riccardosl1
 
AI and Data Privacy in 2025: Global Trends
AI and Data Privacy in 2025: Global TrendsAI and Data Privacy in 2025: Global Trends
AI and Data Privacy in 2025: Global Trends
InData Labs
 
Procurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptxProcurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptx
Jon Hansen
 
Ad

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)