COMP302: Programming Languages and Paradigms
Prof. (Sec 01) Francisco Ferreira (Sec 02)
School of Computer Science Mc
Copyright By PowCoder代写 加微信 powcoder
Week 5-1, Fall 2017
Functional Tidbit: Tuesday (10 Oct)
10 Dec 1815 – 27 Nov 1852 Inventor of the Analytic Engine
Happy (belated) Day!
”The analytical en- gine weaves algebraic patterns just as the Jacquard loom weaves flowers and leaves.”
COMP302: Programming Languages and Paradigms
Functional Tidbit: !
“I find languages that support just one programming paradigm constraining.”
COMP302: Programming Languages and Paradigms 3 / 11
Computation and Effects
Expressions in OCaml have characteristics:
• An expression has a type
• An expression evaluates to a value (or diverges).
Expressions in OCaml may also have an effect.
COMP302: Programming Languages and Paradigms 4 / 11
Recall: Variable Bindings and Overshadowing
Example 1:
let (k : int) = 4;; let(k:int)=3in k*k;; k;;
COMP302: Programming Languages and Paradigms 5 / 11
Recall: Variable Bindings and Overshadowing
Example 1:
1 2 3 4 5 6 7 8 9
Example 2:
COMP302: Programming Languages and Paradigms
let (k : int) = 4;; let(k:int)=3in k*k;; k;;
let pi = 3.14 ;;
let area (r:float) = pi *. r *. r;;
let a2 = area (2.0)
let (pi : float) = 6.0;; let b1 = area (2.0) = a2
let area (r:float) = pi *. r *. r;; let b2 = area (2.0) = a2
How to program with state?
COMP302: Programming Languages and Paradigms 6 / 11
How to program with state? – Demo –
COMP302: Programming Languages and Paradigms 6 / 11
How to program with state? – Allocate and Compare
• How to allocate state? 1 letx=ref0
Allocates a reference cell with the name x in memory and initializes it with 0.
COMP302: Programming Languages and Paradigms 7 / 11
How to program with state? – Allocate and Compare
• How to allocate state? 1 letx=ref0
Allocates a reference cell with the name x in memory and initializes it with 0.
• How to compare two reference cells?
COMP302: Programming Languages and Paradigms
How to program with state? – Allocate and Compare
• How to allocate state? 1 letx=ref0
Allocates a reference cell with the name x in memory and initializes it with 0.
• How to compare two reference cells? Compare their address: r == s
Succeeds, if both r and s are names for the same location in memory
Compare their content: r = s
Succeeds, if both reference cells store the same value.
COMP302: Programming Languages and Paradigms 7 / 11
How to program with state? – Read and Write
• How to read value stored in a reference cell?
Read value that is stored in the ref- erence cell with name x.
Pattern match on value that is stored in the reference cell with name x.
1 let {contents = x} = r
• How to update the value stored in a reference cell? 1 x := 3
Writes the value in the reference cell with the name x The previously stored value is overwritten.
COMP302: Programming Languages and Paradigms
Revisiting Variable Binding and Overshadowing
COMP302: Programming Languages and Paradigms 9 / 11
Imperative Programming in OCaml
let imperative_fact n = begin
let result = ref 1 in let i = ref 0 in
let rec loop () =
if !i = n then ()
else (i := !i + 1; result := !result * !i; loop ()) in
(loop (); !result)
1 2 3 4 5 6 7 8 9
COMP302: Programming Languages and Paradigms
Imperative Programming in OCaml
let imperative_fact n = begin
let result = ref 1 in let i = ref 0 in
let rec loop () =
if !i = n then ()
else (i := !i + 1; result := !result * !i; loop ()) in
(loop (); !result)
1 2 3 4 5 6 7 8 9
• More complicated than the purely functional version • Considered bad style in a functional language
• Harder to reason about its correctnes
COMP302: Programming Languages and Paradigms
Good Uses of State
1 2 3 4 5 6 7
let counter = ref 0
(* newName () ===> a, where a is a new name *) (* Names are described by strings and an nat. *) let newName () =
(counter := !counter + 1;
“a” ^ string_of_int (!counter))
COMP302: Programming Languages and Paradigms
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com