CSC 398 Functional Programming: Instructor: Dr. Hao Wu Lecture 3 Types and Definitions
CSC 398 Functional Programming: Instructor: Dr. Hao Wu Lecture 3 Types and Definitions
Programming
Instructor: Dr. Hao W
fi
Haskell Program/Scripts
• Let’s create another Haskell program called “MyScript.hs
{- ################################
• MyScripts.h
##################################-}
example :: Integer
example = square (size - square (2+2))
:browse Prelude
Modules
Modules
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
Boolean
Boolean
• 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:
Testing
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
fi
fi
Relational operators
Week 1
Negative literals
• 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)
Overloading
Guards
Guards
name x1 x2 … xk
| g1 = e1
| g2 = e2 Results
Guards
…
| otherwise = e
Conditional Expressions
Characters: Char
tab ‘\t’
newline ‘\n’
backslash (\) ‘ \\’
single quote (‘) ‘\’’
double quote (“) ‘\”’
Char
Char
String
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
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