

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An overview of object-oriented programming (oop) and functional programming (fp), comparing their approaches to computation and data. It also includes code examples of implementing a stack abstraction in java and ocaml, highlighting the differences between the two programming paradigms.
Typology: Study notes
1 / 3
This page cannot be seen from the preview
Don't miss anything!


CMSC 330 2
CMSC 330 3
class Stack { class Node { Integer val; Node next; Node(Integer v, Node n) { val = v; next = n; } }; private Node theStack; void push(Integer v) { theStack = new Node(v, theStack); } Integer pop() { if (theStack == null) throw new NoSuchElementException(); Integer temp = theStack.val; theStack = theStack.next; return temp; } }
CMSC 330 4
module type STACK = sig type 'a stack val new_stack : unit -> 'a stack val push : 'a stack -> 'a -> unit val pop : 'a stack -> 'a end
module Stack : STACK = struct type 'a stack = 'a list ref let new_stack () = ref [] let push s x = s := (x::!s) let pop s = match !s with [] -> failwith "Empty stack" | (h::t) -> s := t; h end
CMSC 330 5
let new_stack () = let this = ref [] in let push x = this := (x::!this) and pop () = match !this with [] -> failwith "Empty stack" | (h::t) -> this := t; h in (push, pop)
val s : ('_a -> unit) * (unit -> '_a) = (, )
CMSC 330 6
CMSC 330 7
CMSC 330 8
class C { int x = 0; void set_x(int y) { x = y; } int get_x() { return x; } }
let make () = let x = ref 0 in ( (fun y -> x := y), (fun () -> !x) )
C c = new C(); c.set_x(3); int y = c.get_x();
fun y -> x := y fun () -> !x
let (set, get) = make ();; set 3;; let y = get ();;
CMSC 330 9
class C { f1 ... fn; m1 ... mn; }
let make () = let f1 = ... ... and fn = ... in ( fun ... , (* body of m1 ) ... fun ..., ( body of mn *) )
Tuple containing closures
CMSC 330 10
CMSC 330 11
public interface Function { Integer eval(Integer arg); }
CMSC 330 12
class AddOne implements Function { Integer eval(Integer arg) { return new Integer(arg + 1); } }
class MultTwo implements Function { Integer eval(Integer arg) { return new Integer(arg * 2); } }