this week: programming-in-the-large using the module system of OCaml.

1. STRUCTURING SOFTWARE WITH MODULES

in large project: mangage high number of definitions → abstractions built on top of other abstractions.

  • layers of abstractions: hide information
  • divide program into components
  • identifiers organised to avoid naming conflicts

module as namespace

dot-notation: access module ...

1. IMPERATIVE FEATURES IN OCAML

functional language:

  • immutable data structure
  • identifiers instead of variables
  • pure functions

but imperative features are useful:

  • exceptions to alter control flow
  • ops to consume input and output
  • mutable data structures
  • for and while loop for iterations

2. GETTING AND HANDLING YOUR EXCEPTIONS

  • exceptions in ocaml ...

1. FUNCTIONAL EXPRESSIONS

syntax for functional expr: function some_identifier -> some_expr
the type of the functional expr is t1 -> t2 where t1 is the type of some_identifier, t2 is type of some_expr

ex.

function x -> x + 1;;  
(function x -> 2*x) 5;; (*annonymous function*)   

the previous way of defining function:

let ...

Last week, we only defined flat data structures which are nice to aggregate values but quite limited when you try to structure values.

This week: algebraic datatypes.

1. TAGGED VALUES

⇒ change the return type to a type query_result, which can be either of these:

  • an error
  • a new database (in ...

this week: structure code with types: tuples, records, arrays.

1. USER-DEFINED TYPES

primary use of types: document your code

  • use type type_identifier = some_type to define a new type (type_identifier is synonym/abbrevation of some_type)
  • type_identifier must start with lowercase letter
  • already known types: int, bool, string, char ...
  • use : to add ...

this week: discuss how to handle events in user-interface — MVC, functional reactive programming.

Lecture 4.1 - Imperative Event Handling: The Observer Pattern

Traditional way of handling events: observer Pattern (MVC). Used when views need to react to change in a model.

MVC: model-view-controller for user interface

  • Views can announce themselves ...

This week: scala for imperative programming.

Lecture 3.1 - Functions and State

So far: pure functional programming
→ side-effect free: therefore time doesn't matter.
Any rewriting that terminates lead to the same solution. (Churcher-Rosser Th)

Now: mutable states

Stateful objects: objects can have state that change over time. (state is ...

1. BASIC DATA TYPES: int, bool

Rich type system and polymorphism in ocaml. Types are infered not declared.

Basic types: int, bool, float, string, char, ...

int

value: \(-2^{62}\) ~ \(2^{62}-1\) on 64-bit machines.
ops: +, -, *, /, mod (reminder: / is integer division)

# 3+2*4-1;; 
- : int = 10 
# 5/2;; 
- : int ...

Lecture 2.1 - Structural Induction on Trees (optional)

Generalize the structural induction on list to general structures like trees.

To prove a property P(t) for all trees t:
* show for any leave l, P(l) holds
* for each internal node t with subtrees s1...sn, show P(s1)&...&P ...