CS计算机代考程序代写 Java Haskell CPSC 312 2021 – PreClass Video, Lecture 2

CPSC 312 2021 – PreClass Video, Lecture 2
©D. Poole 2021
CPSC 312 — Pre Lecture 2 1/11

Haskell Syntax
comments are either
— comment to end of line or {- comment -}
variables either:
􏰌 prefix: made up of letters, digits, ’ or lower-case letter
and start with a
􏰌 infix: made up of sequences of other characters indentation is significant
parentheses are used for precedence and tuples (not for arguments of functions)
Function application binds most strongly
factorial 3*5 means
(factorial 3)*5
Binary prefix functions can be made infix using back-quotes, e.g. ‘div‘
Infix operators can be made prefix using parentheses, e.g. (*)
CPSC 312 — Pre Lecture 2 2/11
©D. Poole 2021

Definitions
= provides definitions.
x=7
Function Definition:
name x1 x2 … xk = e
x1 x2 … xk are formal parameters e is an expression
xi can contain constants and structures, but each variable can only appear once.
Multiple equations can define a function; the first one to succeed is used.
CPSC 312 — Pre Lecture 2 3/11
©D. Poole 2021

Evaluation of Haskell program
Haskell evaluates expressions.
Haskell knows how to implement some expressions
(such as 3+4*7)
Given the defintion of name:
name x1 x2 … xk = e
The expression
name v1 v2 … vk
when all k argumnents are provided evaluates to value of e {x1/v1, x2/v2, …, xk/vk}
which is same as e but with each xi replaced with vi Example: x*1000+y {x/9, y/3} evaluates to value of 9*1000+3 which is 9003.
foo x y = 1000*x+y
foo 9 3
bar = foo 7
bar 3
CPSC 312 — Pre Lecture 2 4/11
©D. Poole 2021

Type Declarations
Function defintion
name x1 x2 … xk = e
Can have type declaration:
name :: t1 -> t2 -> … -> tk -> t
ti istypeofei,andt isthetypeofe.
Each function takes only one argument:
name v1isafunctionoftype t2 -> … -> tk -> t name v1 v2 … vk isavalueoftypet
It’s value is the value of e with each x1 replaced by vi
CPSC 312 — Pre Lecture 2 5/11
©D. Poole 2021

Haskell Types:
Bool (&&, ||, not) Num (+, −, ∗, abs)
Integral (div, mod) Int
Integer Fractional (/)
Floating (log, sin, exp, …) Double
Eq (==, /=)
Ord (>, >=, <=, <) Char String CPSC 312 — Pre Lecture 2 6/11 ©D. Poole 2021 Type: Bool Bool is a type with two values True and False. operations: && and || or not not How can we define exclusive-or (xor)? How can we define if-then-else? What would happen if we tried to do this in Java? CPSC 312 — Pre Lecture 2 7/11 ©D. Poole 2021 Integral types Intergral types represent integers. They implement + * ^ - div mod abs negate Two implementations: 􏰌 Int - fixed-precision integers 􏰌 Integer - arbitrary precision integers Integral is a class. Int and Integer are types in class Integral. Only types have implementations. (Haskell classes are like Java interfaces) div :: Integral a => a -> a -> a
Means div takes two arguments of the same type, and returns a value of that type.
That type must be in the Integral class.
CPSC 312 — Pre Lecture 2 8/11
©D. Poole 2021

Fractional types
Fractional types represent real numbers. They implement + * ^ – / abs negate
Floating types also implement log sin exp . . . Multiple implementations (these are types):
􏰌 Double – double precision floating-point numbers (64 bit) 􏰌 Float – single precision floating-point numbers (32 bit)
— don’t use
􏰌 Rational – exact rational numbers
There are no types that are both Integral and Fractional.
Num types implement + * ^ – abs negate Num is a class (elements are types).
Integral and Fractional are subclasses of Num
CPSC 312 — Pre Lecture 2 9/11
©D. Poole 2021

Eq and Ord classes
Eq types implement == /=
Ord types implement > >= <= < max min Int, Integer, Double implement Eq and Ord Can you think of a Num type that isn’t an Ord type? CPSC 312 — Pre Lecture 2 10 / 11 ©D. Poole 2021 ©D. Poole 2021 CPSC 312 — Pre Lecture 2 11 / 11