1 Basics
1 Basics
Yuepeng Wang
Spring 2025
1
Overview
• Introduction to functional programming
• Haskell basics
• Type declarations
• Type variables
• Type classes
2
Imperative Programming
• For imperative languages, a program gives a sequence of commands/
statements that the computer can execute in order
3
Declarative Programming
• In some languages, a program does not specify a sequence of commands
SELECT Name, Address, City, Province
FROM Customers
• A program only specifies what we want to obtain, but not how we can
obtain it
• The return value of a function is all that matters (no side effects)
6
Homework 0
• Install the GHC compiler (>= 9.2 recommended)
https://www.haskell.org/ghc/download_ghc_9_2_1.html
• No submission
7
Using GHCi
• In GHCi, we can use ":l" to load a file containing Haskell source code
ghci> :l test.hs
ghci> :r
8
Prelude
• A module called Prelude is loaded to the context by default, for both GHC
and GHCi
9
Values
• Basic values and types
10
Arithmetic Expressions
Expression Result
2-4 -2
2*3+4*5 26
2^3*4 32
4.0 + 2 6.0
4.0 / 2 2.0
4/2 2.0
4 `div` 2 2
11
Logical Expressions
Expression Result
4 == 2 False
4 /= 2 True
4 <= 2 False
12
Lists
• Conceptually, Haskell lists are singly-linked lists
13
List Cons
• A common list operation is cons (which means "construct")
Expression Result
1 : [2, 3] [1, 2, 3]
1 : (2 : (3 : [ ])) [1, 2, 3]
1:2:3:[] [1, 2, 3]
• The last two rows are the same because the : operator is right-associative
14
List Append
• Another common list operation is append
• It takes as input two lists and produces as output a new list with the
second list appended to the first
Expression Result
[1, 2] ++ [3, 4] [1, 2, 3, 4]
[ ] ++ [1, 2] [1, 2]
[1, 2] ++ [ ] [1, 2]
15
Characters and Strings
• Characters are surrounded with single quotes
Expression Result
'abc' Error
16
Strings
• Strings are syntactic sugar of lists of characters
Expression Result
17
Functions
• Functions are basic units of functional programming languages
18
Functions
• In general, Haskell functions use the prefix form
Mathematics Haskell
f(x) fx
f(x, y) fxy
f(x, y) + 1 f x y + 1 or (f x y) + 1
f(x, g(y)) f x (g y)
f(g(x), y) f (g x) y
19
Functions
• Binary functions to infix form: add backquotes
ghci> div 4 2
2
ghci> 4 `div` 2
2
20
Functions
• Functions are usually defined in a Haskell source file, e.g., test.hs
double x = x * 2
add x y = x + y
list x y = [x, y]
22
Type Names
• Type names must start with an upper case letter in Haskell
23
Common Haskell Types
63 63
• Int: bounded, whole numbers, −2 to 2 − 1 for 64-bit machines
24
Common Haskell Types
• Tuple: definition depends on the length and types of components
26
Type Declarations
• A function also has its type, which is different from the return type
• Example
half :: Float -> Float
half x = x / 2
27
Type Declarations
• We can also specify functions with multiple parameters
28
Type Variables
• Some functions operate on various types
• For example, list append (++) works on list of integers, characters, etc.
30
Example: Type Variables
• head is a function from Prelude that takes a non-empty list and returns the
first element ghci> head [1, 2, 3]
1
• tail is a function from Prelude that takes a non-empty list and returns a list
with the first element removed ghci> tail [1, 2, 3]
[2, 3]
31
Type Classes
• Sometimes we do not want an arbitrary type. We may need to add some
constraints to the type
32
Type Classes
• What is the type of (==)?
ghci> :t (==)
(==) :: Eq a => a -> a -> Bool
• Meaning
• The (==) function takes as input two values of the same type and
returns a boolean value
• The type of those two values must be an instance of the Eq type class
33
Type Class: Eq
• Eq is used for types that support equality comparison
34
Type Class: Ord
• Ord is used for types whose values are in a total order (any two values
are comparable)
• It covers all standard comparison functions such as >, <, >=, and <=
• All the types we have covered so far (except for functions) are instances
of the Ord type class
ghci> :t (>)
(>) :: Ord a => a -> a -> Bool
35
Type Class: Show
• Values whose types are instances of Show can be represented as strings
• All the types we have covered so far (except for functions) are instances
of the Show type class
36
Type Class: Read
• The opposite of type class Show. Values whose types are instances of
Read can be constructed from strings
• All the types we have covered so far (except for functions) are instances
of the Read type class
37
Type Class: Num
• Num is a numeric type class. Its instances act like numbers
38
Type Class: Integral
• Integral is another numeric type class that only includes integral/whole
numbers. It includes types Int and Integer.
• Example: the built-in length function returns the length of a list, which has
type of Int
ghci> fromIntegral (length [1, 2]) + 4.2
6.2
39