CS代考 COMP 302: Programming Languages and Paradigms

Week 6: Exceptions Prof. Xujie Si
COMP 302: Programming Languages and Paradigms

Midterm Exam (online)

Copyright By PowCoder代写 加微信 powcoder

• Scope: topics covered in week 1 – 6
• Date: March 10, 8 AM – March 12, 8 PM • Expected duration: T = 75 minutes
To account for both accommodations with OSD and possible internet connectivity issues
Actual time window: (2xT + 30 minutes) = 180 minutes

Computation and Effects

Computation and Effects
• An expression evaluates to a value (or diverges)
• An expression may have an effect • Update memory
• Raise an exception
• Print logs, warm your CPUs, etc.

Computation and Effects
• An expression evaluates to a value (or diverges) let f x = (let r = x * x in fun y -> r + y)
let v = f 2
What is the value and type of v?
• An expression may have an effect • Update memory
• Raise an exception
• Print logs, warm your CPUs, etc.

Computation and Effects
• An expression evaluates to a value (or diverges) let f x = (let r = x * x in fun y -> r + y)
let v = f 2
What is the value and type of v?
• An expression may have an effect • Update memory
• Raise an exception
• Print logs, warm your CPUs, etc.

Computation and Effects
• An expression evaluates to a value (or diverges) let f x = (let r = x * x in fun y -> r + y)
let v = f 2
What is the value and type of v?
• An expression may have an effect • Update memory
• Raise an exception
• Print logs, warm your CPUs, etc.

Computation and Effects
• An expression evaluates to a value (or diverges) let f x = (let r = x * x in fun y -> r + y)
let v = f 2
What is the value and type of v?
• An expression may have an effect • Update memory
• Raise an exception
• Print logs, warm your CPUs, etc.

Computation and Effects
• An expression evaluates to a value (or diverges) let f x = (let r = x * x in fun y -> r + y)
let v = f 2
What is the value and type of v?
• An expression may have an effect • Update memory
• Raise an exception
• Print logs, warm your CPUs, etc.

Examples on type, value, and effect
Expression: 3 / 0

Examples on type, value, and effect
Expression: 3 / 0 Type? Value? Effect?

Examples on type, value, and effect
Expression: 3 / 0 Type? int Value?

Examples on type, value, and effect
Expression: 3 / 0 Type? int (/) : int -> int -> int Value?

Examples on type, value, and effect
Expression: 3 / 0 Type? int (/) : int -> int -> int Value? N/A

Examples on type, value, and effect
Expression: 3 / 0 Type? Value? Effect?
int (/) : int -> int -> int N/A
Raise a run-time exception “Division_by_zero”

Examples on type, value, and effect
Expression: 3 / 0 Type? Value? Effect?
Expression: fun y -> 3 / 0 + y
int (/) : int -> int -> int N/A
Raise a run-time exception “Division_by_zero”

Examples on type, value, and effect
Expression: 3 / 0 Type? Value? Effect?
Expression: fun y -> 3 / 0 + y Type?
Value? Effect?
int (/) : int -> int -> int N/A
Raise a run-time exception “Division_by_zero”

Examples on type, value, and effect
Expression: 3 / 0 Type? Value? Effect?
Expression: fun y -> 3 / 0 + y Type?
int (/) : int -> int -> int N/A
Raise a run-time exception “Division_by_zero”
int -> int Value?

Examples on type, value, and effect
Expression: 3 / 0 Type? Value? Effect?
Expression: fun y -> 3 / 0 + y Type?
Value? Effect?
int (/) : int -> int -> int N/A
Raise a run-time exception “Division_by_zero”
int -> int
fun y -> 3 / 0 + y

Examples on type, value, and effect
Expression: 3 / 0 Type? Value? Effect?
Expression: fun y -> 3 / 0 + y Type?
Value? Effect?
int (/) : int -> int -> int N/A
Raise a run-time exception “Division_by_zero”
int -> int
fun y -> 3 / 0 + y
No effects

Examples on type, value, and effect
Expression: 3 / 0 Type? Value? Effect?
Expression: fun y -> 3 / 0 + y Type?
Value? Effect?
int (/) : int -> int -> int N/A
Raise a run-time exception “Division_by_zero”
int -> int
fun y -> 3 / 0 + y
No effects
Expression: (fun y -> 3 / 0 + y) 2

Examples on type, value, and effect
Expression: 3 / 0 Type? Value? Effect?
Expression: fun y -> 3 / 0 + y Type?
Value? Effect?
int (/) : int -> int -> int N/A
Raise a run-time exception “Division_by_zero”
int -> int
fun y -> 3 / 0 + y
No effects
Expression: (fun y -> 3 / 0 + y) 2
Similar to the first example

Examples on type, value, and effect
Expression:
let head (x::t) = x in head []

Examples on type, value, and effect
Expression:
let head (x::t) = x in head []
Type? Value? Effect?

Examples on type, value, and effect
Expression:
let head (x::t) = x in head []
Type? Value? Effect?

Examples on type, value, and effect
Expression:
let head (x::t) = x in head []
Type? Value? Effect?

Examples on type, value, and effect
Expression:
let head (x::t) = x in head []
Type? Value? Effect?
Raise a run-time exception “Match_failure”

Examples on type, value, and effect
Expression:
let head (x::t) = x in head []
Type? Value? Effect?
Expression:
Printf.printf “hello world”
Raise a run-time exception “Match_failure”

Examples on type, value, and effect
Expression:
let head (x::t) = x in head []
Type? Value? Effect?
Expression:
Printf.printf “hello world”
Type? Value? Effect?
Raise a run-time exception “Match_failure”

Examples on type, value, and effect
Expression:
let head (x::t) = x in head []
Type? Value? Effect?
Expression:
Printf.printf “hello world”
Type? unit Value?
Raise a run-time exception “Match_failure”

Examples on type, value, and effect
Expression:
let head (x::t) = x in head []
Type? Value? Effect?
Expression:
Printf.printf “hello world”
Type? unit Value? () Effect?
Raise a run-time exception “Match_failure”

Examples on type, value, and effect
Expression:
let head (x::t) = x in head []
Type? Value? Effect?
Expression:
Printf.printf “hello world”
Type? unit Value? ()
Raise a run-time exception “Match_failure”
Print out a string to the screen, which then emits photons to your eyes

User-defined Exceptions
“exception name must start with a Capital letter”

User-defined Exceptions
exception MyException
“exception name must start with a Capital letter”

User-defined Exceptions
exception MyException
“exception name must start with a Capital letter”

User-defined Exceptions
exception MyException
“exception name must start with a Capital letter”

User-defined Exceptions
exception MyException
“exception name must start with a Capital letter”

User-defined Exceptions
exception MyException
“exception name must start with a Capital letter”
What is the type of “raise NotImplemented”?

User-defined Exceptions
exception MyException
“exception name must start with a Capital letter”
What is the type of “raise NotImplemented”?
raise : exn -> ’a

User-defined Exceptions
exception MyException
“exception name must start with a Capital letter”
What is the type of “raise NotImplemented”?
raise : exn -> ’a
NotImplemented : exn
MyException : exn

Catch an exception

Catch an exception
Expression:
with ExcName ->

Catch an exception
Expression:
with ExcName ->
Note that and should have the same type!!

Catch an exception
Expression:
with ExcName ->
Note that and should have the same type!!

Catch an exception
Expression:
with ExcName ->
Note that and should have the same type!!
Type Error!!
“() does not belong to type bool”

Exception with extra information

Exception with extra information
Note that we do need a parenthesis here!
“raise Err x” does not work. Why?

Backtrack using exception
Find an element in a tree (which may or may not be a binary search tree)

Backtrack using exception
Find an element in a tree (which may or may not be a binary search tree)

Backtrack using exception
Find an element in a tree (which may or may not be a binary search tree)

• Implement a function
change : int list -> int -> int list
Types of coins Total amount A change plan
# change [5; 2; 1] 13
– : int list = [5; 5; 2; 1]

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com