Some Haskell Classes and Types
Bool (&&, ||, not) Num (+, −, ∗, abs)
Integral (div, mod) Int
Integer Fractional (/)
Floating (log, sin, exp, …) Double
Eq (==, /=)
Ord (>, >=, <=, <)
List ([] 🙂 Char
String
©D. Poole 2019
CPSC 312 — Lecture 3 1/6
Guards
Guards are used for if-then-else structure in definition of functions.
Example
mymax x y
| x>y = x
| otherwise = y
CPSC 312 — Lecture 3 2/6
©D. Poole 2019
Guards
Guards are used for if-then-else structure in definition of functions.
Example
mymax x y
| x>y = x
| otherwise = y
It evaluates the guards; the first one succeeding, the corresponding expression is returned
CPSC 312 — Lecture 3 2/6
©D. Poole 2019
Guards
General case:
name x1 x2 … xk
| g1 = e2
| g2 = e2 …
| gn = en
evaluate g1, g2 in turn until the first one gi evaluates to true, then return value of ei .
CPSC 312 — Lecture 3 3/6
©D. Poole 2019
Guards
General case:
name x1 x2 … xk
| g1 = e2
| g2 = e2 …
| gn = en
evaluate g1, g2 in turn until the first one gi evaluates to true, then return value of ei .
An Exception is raised if none of the guards are True
Typical to have last condition to be otherwise which is a variable with value True.
CPSC 312 — Lecture 3 3/6
©D. Poole 2019
Guards
General case:
name x1 x2 … xk
| g1 = e2
| g2 = e2 …
| gn = en
evaluate g1, g2 in turn until the first one gi evaluates to true, then return value of ei .
An Exception is raised if none of the guards are True Typical to have last condition to be otherwise which is a
variable with value True.
How can we implement max3?
CPSC 312 — Lecture 3 3/6
©D. Poole 2019
Guards
General case:
name x1 x2 … xk
| g1 = e2
| g2 = e2 …
| gn = en
evaluate g1, g2 in turn until the first one gi evaluates to true, then return value of ei .
An Exception is raised if none of the guards are True Typical to have last condition to be otherwise which is a
variable with value True.
How can we implement max3?
Haskell also has “if … then … else …” structure
CPSC 312 — Lecture 3 3/6
©D. Poole 2019
Lists
A list is an ordered sequence of elements of the same type
CPSC 312 — Lecture 3 4/6
©D. Poole 2019
Lists
A list is an ordered sequence of elements of the same type A list of type [t], where t is a type, is either:
the empty list []
oftheformh:rwherehisoftypetandrisalistoftype[t]
and : is an infix function.
CPSC 312 — Lecture 3 4/6
©D. Poole 2019
Lists
A list is an ordered sequence of elements of the same type A list of type [t], where t is a type, is either:
the empty list []
oftheformh:rwherehisoftypetandrisalistoftype[t]
and : is an infix function.
A list has a special syntax :
[7] is an abbreviation for 7:[]
[5,7] is an abbreviation for 5:7:[] [3,5,7] is an abbreviation for 3:5:7:[] : associates to the right
CPSC 312 — Lecture 3 4/6
©D. Poole 2019
Lists
A list is an ordered sequence of elements of the same type A list of type [t], where t is a type, is either:
the empty list []
oftheformh:rwherehisoftypetandrisalistoftype[t]
and : is an infix function.
A list has a special syntax :
[7] is an abbreviation for 7:[]
[5,7] is an abbreviation for 5:7:[] [3,5,7] is an abbreviation for 3:5:7:[] : associates to the right
both […] and : notation can be used in patterns on the left side of =.
Head and tail functions can be defined by:
head (h:t) = h
tail (h:t) = t
CPSC 312 — Lecture 3 4/6
©D. Poole 2019
Examples (Lists.hs)
myappend l1 l2
returns the list containing the elements of list l1 followed by elements of l2
CPSC 312 — Lecture 3 5/6
©D. Poole 2019
Examples (Lists.hs)
myappend l1 l2
returns the list containing the elements of list l1 followed by elements of l2
This can also be defined as infix function
l1 ++++ l2
returns the list containing the elements of list l1 followed by elements of l2
CPSC 312 — Lecture 3 5/6
©D. Poole 2019
Examples (Lists.hs)
myappend l1 l2
returns the list containing the elements of list l1 followed by elements of l2
This can also be defined as infix function
l1 ++++ l2
returns the list containing the elements of list l1 followed by elements of l2
A string is a list of characters
type String = [Char] — Defined in ‘GHC.Base’
CPSC 312 — Lecture 3 5/6
©D. Poole 2019
Examples (Lists.hs)
myappend l1 l2
returns the list containing the elements of list l1 followed by elements of l2
This can also be defined as infix function
l1 ++++ l2
returns the list containing the elements of list l1 followed by elements of l2
A string is a list of characters
type String = [Char] — Defined in ‘GHC.Base’
[1,2,3] ++++ [’a’,’b’]
gives an error. Why?
CPSC 312 — Lecture 3 5/6
©D. Poole 2019
Examples (Lists.hs)
myappend l1 l2
returns the list containing the elements of list l1 followed by elements of l2
This can also be defined as infix function
l1 ++++ l2
returns the list containing the elements of list l1 followed by elements of l2
A string is a list of characters
type String = [Char] — Defined in ‘GHC.Base’
[1,2,3] ++++ [’a’,’b’]
gives an error. Why?
The standard Prelude defines ++ for append.
CPSC 312 — Lecture 3 5/6
©D. Poole 2019
©D. Poole 2019
CPSC 312 — Lecture 3 6/6