0% found this document useful (0 votes)
46 views

CSC 398 Functional Programming: Instructor: Dr. Hao Wu Lecture 3 Types and Definitions

The document discusses Haskell modules and types. It introduces modules and how they are used to organize code. It also covers built-in types like integers, booleans, and their corresponding operations.

Uploaded by

Cleo Brown
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

CSC 398 Functional Programming: Instructor: Dr. Hao Wu Lecture 3 Types and Definitions

The document discusses Haskell modules and types. It introduces modules and how they are used to organize code. It also covers built-in types like integers, booleans, and their corresponding operations.

Uploaded by

Cleo Brown
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

CSC 398 Functional

Programming
Instructor: Dr. Hao W

Lecture 3 Types and De nitions


u

fi
Haskell Program/Scripts
• Let’s create another Haskell program called “MyScript.hs
{- ################################
• MyScripts.h
##################################-}

module MyScripts where Comments:


-- The variable size has a value of 25
• “—“: in line
size :: Integer comments
size = 25
• “{-“ and “-}”: nested
-- The function to square an integer
comments,
square :: Integer -> Integer
square n = n*n

-- An example using size and square

example :: Integer
example = square (size - square (2+2))

2 Hao Wu, CSC 398 Functional Programming Week 1


The standard prelude and The Haskell Libraries

• Haskell has various built-in type


• Definitions of these are contained in a file, the standard
prelude, Prelude.h
• When Haskell is used, the default is to load the standard
prelud
• try the GHCi command to see the definitions:

:browse Prelude

3 Hao Wu, CSC 398 Functional Programming Week 1


e

Modules

• A typical piece of computer software will contain thousands of


lines of program tex
• To make this manageable, we split it into smaller components
called “Modules
• A module has a name and contain a collection of definition
• To introduce a module
module MyScripts where

• A module may also import definitions from other modules

module NewScripts where


• import MyScripts

4 Hao Wu, CSC 398 Functional Programming Week 1


Modules

• The module mechanism allows us to control how definitions


are imported and also which definitions are made available or
exported by a module for use by other modules.

5 Hao Wu, CSC 398 Functional Programming Week 1


The Booleans: Bool
• The Boolean operations provided in the languages are
&& an
|| o
not. no

Truth table

t1 t2 t1 && t2 t1 | | t2 t1 not t1
T T T T T F
T F F T F T
F T F T
F F F F

6 Hao Wu, CSC 398 Functional Programming Week 1


r

Boolean

• Inclusive-Or: build-in “| |”, returns True if either one or both of


its arguments are Tru
• Exclusive-Or: returns True when exactly one but not both of its
arguments has the value Tru

exOr :: Bool -> Bool -> Bool
exOr x y = (x||y) && not (x && y)

7 Hao Wu, CSC 398 Functional Programming Week 1


e

Boolean

• Boolean values can also be compared for equality and


inequality using the operators “==“ and “/=“, which both have
the typ
Bool -> Bool -> Bool

• Note that “/=“ is the same function as exOr


8 Hao Wu, CSC 398 Functional Programming Week 1


e

Literals and definitions

• Literals:
- values which are given literall
- need no evaluatio
- the result of evaluating a literal is the literal itsel
- True or False, 2, 3
• We can use literals True and False as arguments
myNot :: Bool -> Bool
myNot True = False
myNot False = True
• We can also use a combination of literals and variables on the
left-hand side of equations:

exOr True x = not x


exOr False x = x

9 Hao Wu, CSC 398 Functional Programming Week 1


Testing

• We can test myNot against the built in function

prop_myNot :: Bool -> Bool


prop_myNot x =
not x == my Not x

10 Hao Wu, CSC 398 Functional Programming Week 1


Integers
• Integer type contains the integers: positive, zero and negativ
• We do arithmetic on integers using the following operators and
function
+ The sum of two integers
* The product of two integers
^ Raise to the power;2^3 is 8
The difference of two integers, when in x: a-b; The
-
integer of opposite sign, when pre x: -a
Whole number division; for example div 14 3 is 4. This
div
can also be written 14 ‘div’ 3
The remainder from whole number division; for
mod
example mod 14 3 (or 14 ‘mod’ 3) is 2
abs The absolute value of an integer; remove the sign
negate The function to change the sign of an integer

- Functions surrounded by backquotes is written between its two


arguments is an infix version of the function.

11 Hao Wu, CSC 398 Functional Programming Week 1


s

fi
fi

Relational operators

• These functions take two integers as input and return a Bool

> greater than (and not equal to)


>= greater than or equal to
== equal to
/= not equal to
<= less than or equal to
< less than (and not equal to)

• Example to test whether three Integers are equal


threeEqual :: Integer -> Integer -> Integer -> Bool
threeEqual m n p = (m == n) && (n == p)

12 Hao Wu, CSC 398 Functional Programming

Week 1

Negative literals

• Negative literals cause problems in Haskel


• For example
- The number minus twelve is written as -1
- The prefix ‘-‘ can often get confuse with the infix operator to subtract
one number from anothe
- negate - 34 is interpreted as ‘negate minus 34’ and leads to error

• If you are in any doubt about the source of an error and you
are dealing with negative numbers, you should enclose them
in parentheses.
• negate (-34)

13 Hao Wu, CSC 398 Functional Programming Week 1


:

Fixed-size integer: the Int type

• The Int type represents integers in a fixed amount of spac


- it can only represent a finite range of integers.
- The value maxBound gives the greatest value in the type:
214748364
- it is more efficient
• If larger numbers may be required, its better to use the Integer
type

14 Hao Wu, CSC 398 Functional Programming Week 1


7

Overloading

• Using the same symbol or name for different operations is


called overloadin
• The symbol “==“ can be used to compare the equality for
Integers, Ints and Booleans
• This means “==“ will have the type
Integer -> Integer -> Bool
Int -> Int -> Bool
Bool -> Bool -> Bool

• Many symbols in Haskell are overloaded

15 Hao Wu, CSC 398 Functional Programming Week 1


Guards

• A guard (condition) is a Boolean expressio


- used to express various cases in the definition of a functio

• Example of the function to return the maximum value of two


integers
max :: Integer -> Integer -> Integer
max x y
| x >= y = x
| otherwise = y

If this guard is If the guard is


True then the False then the
result is x result is y

16 Hao Wu, CSC 398 Functional Programming Week 1


Guards

• The general form for function definitions with guards

Name of the function


Formal Parameters
being defined

name x1 x2 … xk
| g1 = e1
| g2 = e2 Results
Guards

| otherwise = e

17 Hao Wu, CSC 398 Functional Programming Week 1

Conditional Expressions

• We can also write general conditional expressions by the “if…


then…else” construc

if condition then m else n

• Maximum of two integers

max :: Integer -> Integer -> Integer


max x y =
if x >= y then x else y

18 Hao Wu, CSC 398 Functional Programming Week 1


Characters: Char

• Literal characters are written inside single quote


- Examples: ‘d’ is the Haskell representative of the characte
- ‘3’ is the character thre
- Some special characters

tab ‘\t’
newline ‘\n’
backslash (\) ‘ \\’
single quote (‘) ‘\’’
double quote (“) ‘\”’

- Standard coding for characters as integers — ASC II coding

19 Hao Wu, CSC 398 Functional Programming Week 1


:

Char

• There are conversion functions between characters and their


numerical code
fromEnum :: Char -> Int
toEnum :: Int -> Char

• To convert a small letter to a capital lette



offset :: Int
offset = fromEnum 'A' - fromEnum 'a'

toUpper :: Char -> Char


toUpper ch = toEnum (fromEnum ch + offset)

20 Hao Wu, CSC 398 Functional Programming Week 1


s

Char

• Check whether a character is a digit

isDigit :: Char -> Bool


isDigit ch = ('0' <= ch) && ('9' >= ch)

21 Hao Wu, CSC 398 Functional Programming Week 1


String

• The String type is made up of sequences of characters, written


between double quote
- Examples: “haskell”, “double”, “haskell\tdouble

• Output string on the scree


- If we evaluate string in GHCi, the result is exactly the same as the
inpu
- The output operation is done using the primitive Haskell functio
putStr :: String -> IO ()
- or
putStrLn :: String -> IO ()
• String can also be joined together using “++”

22 Hao Wu, CSC 398 Functional Programming Week 1


Name, string and characters

is a name or a variable, if
a
de ned it may have any type
‘a’ is a character, so of type Char
“a” is a string, and of type String

23 Hao Wu, CSC 398 Functional Programming Week 1


fi
String and Values

• Build-in functions show and read can convert from a value to a


String and vice vers

show (2+3) -> “5”


show (True || False) -> “True”

read “True” -> True


read “3” -> 3
• In some situations it will not be clear what should be the result
type for read — it is then possible to give a type to the
applicatio
• (read “3” ) :: Int

24 Hao Wu, CSC 398 Functional Programming Week 1


n

Floating-point numbers: Float

• Float: numbers with fractional part


- Float has a fixed amount of space
- not all fractions can be represented by float, then the arithmetic over
them will not always be exac
• Double : double -precision floating-point number
• Haskell allows literal floating-point numbers in scientific
notatio
- 231.61e7 —> 231.61 x 107 = 2,316,100,000

25 Hao Wu, CSC 398 Functional Programming Week 1


n

Converting Integer to Float

26 Hao Wu, CSC 398 Functional Programming Week 1


Converting Integer to Float

27 Hao Wu, CSC 398 Functional Programming Week 1


Syntax: Definitions and layout

• In Haskell the layout of the program is used to state where one


definition ends and the next begin
• Formally, a definition is ended by the first piece of text which
lies at the same indentation or to the left of the start of the
definition

28 Hao Wu, CSC 398 Functional Programming Week 1


s

Syntax: Names
• Names used in definitions of values
- must begin with a small lette
- followed by an optional sequence of letters, digits, underscores and
single quotes
• Type, constructors, module and type classes name
- begin with capital letter

• Reserved words which CANNOT be used


- case class data default deriving do else if import in infix infill infix
instance let module new type of then type where

29 Hao Wu, CSC 398 Functional Programming Week 1


s

You might also like