Docsity

Final Exam Review - Organization Programming Language | CMSC 330, Study notes of Programming Languages

Partial preview of the text

Download Final Exam Review - Organization Programming Language | CMSC 330 and more Study notes Programming Languages in PDF only on Docsity!

CMSC 330: Organization of

Programming Languages

Final Exam Review

Review Choices

• OCaml

– closures, currying, etc

• Threads

– data races, synchronization, classic probs

• Java Generics

• Topics

– garbage collection, exceptions, parameters

• Semantics and Lambda Calculus

Example

let add x = (fun y -> x + y)

(add 3) 4 → 4 →^^3 +^4 → 7

Curried Functions in OCaml

• OCaml has a really simple syntax for currying

  • (^) This is identical to all of the following:

• Thus:

  • (^) add has type int -> (int -> int)
  • (^) add 3 has type int -> int
    • (^) The return of add x evaluated with x = 3
    • (^) add 3 is a function that adds 3 to its argument
  • (^) (add 3) 4 = 7

• This works for any number of arguments

let add x y = x + y

let add = (fun x -> (fun y -> x + y)) let add = (fun x y -> x + y) let add x = (fun y -> x+y)

Another Example of Currying

• A curried add function with three arguments:

  • (^) The same as

• Then...

  • (^) add_th has type int -> (int -> (int -> int))
  • (^) add_th 4 has type int -> (int -> int)
  • (^) add_th 4 5 has type int -> int
  • (^) add_th 4 5 6 is 15

let add_th x y z = x + y + z

let add_th x = (fun y -> (fun z -> x+y+z))

Data Types

• Rect and Circle are type constructors- here a

shape is either a Rect or a Circle

• Use pattern matching to deconstruct values, and

do different things depending on constructor

type shape = Rect of float * float (* width * length ) | Circle of float ( radius *)

let area s = match s with Rect (w, l) -> w *. l | Circle r -> r *. r *. 3.

area (Rect (3.0, 4.0)) area (Circle 3.0)

Polymorphic Data Types

• This option type can work with any kind of

data

  • (^) In fact, this option type is built-in to OCaml

type 'a option = None | Some of 'a

let add_with_default a = function None -> a + 42 | Some n -> a + n

add_with_default 3 None (* 45 ) add_with_default 3 (Some 4) ( 7 *)

Recursive Data Types

• Do you get the feeling we can build up lists this

way?

  • (^) Note: Don’t have nice [1; 2; 3] syntax for this kind of

list

type 'a list = Nil | Cons of 'a * 'a list

let rec length l = function Nil -> 0 | Cons (_, t) -> 1 + (length t)

length (Cons (10, Cons (20, Cons (30, Nil))))

Module Signatures

module type FOO = sig val add : int -> int -> int end;;

module Foo : FOO = struct let add x y = x + y let mult x y = x * y end;;

Foo.add 3 4;; (* OK ) Foo.mult 3 4;; ( not accessible *)

Entry in signature Supply function types

Give type to module

Abstract Types in Signatures

• Now definition of shape is hidden

module type SHAPES = sig type shape val area : shape -> float val unit_circle : shape val make_circle : float -> shape val make_rect : float -> float -> shape end;;

module Shapes : SHAPES = struct ... let make_circle r = Circle r let make_rect x y = Rect (x, y) end

Semicolon Revisited; Side

Effects

• Now that we can update memory, we have a real

use for ; and () : unit

  • (^) e1; e2 means evaluate e1, throw away the result, and

then evaluate e2, and return the value of e

  • (^) () means “no interesting result here”
  • (^) It’s only interesting to throw away values or use () if

computation does something besides return a result

• A side effect is a visible state change

  • (^) Modifying memory
  • (^) Printing to output
  • (^) Writing to disk

Exceptions

exception My_exception of int

let f n = if n > 0 then raise (My_exception n) else raise (Failure "foo")

let bar n = try f n with My_exception n -> Printf.printf "Caught %d\n" n | Failure s -> Printf.printf "Caught %s\n" s

Thread Creation in Java

• To explicitly create a thread:

– Instantiate a Thread object

  • (^) An object of class Thread or a subclass of

Thread

– Invoke the object’s start() method

  • (^) This will start executing the Thread’s run()

method concurrently with the current thread

– Thread terminates when its run() method

returns

Data Race Example

public class Example extends Thread {

private static int cnt = 0; // shared state

public void run() {

int y = cnt;

cnt = y + 1;

public static void main(String args[]) {

Thread t1 = new Example();

Thread t2 = new Example();

t1.start();

t2.start();