Docsity

OCaml Functional Programming in CMSC 330: Call Stacks, Scoping, and Currying - Prof. Atif , Study notes of Programming Languages

Partial preview of the text

Download OCaml Functional Programming in CMSC 330: Call Stacks, Scoping, and Currying - Prof. Atif and more Study notes Programming Languages in PDF only on Docsity!

CMSC 330: Organization of

Programming Languages

Functional Programming with OCaml

CMSC 330 2

Reminders

• Homework 2 will be posted soon

CMSC 330 3

Review

• Recursion is how all looping is done

• OCaml can easily pass and return functions

CMSC 330 4

The Call Stack in C/Java/etc.

void f(void) { int x; x = g(3); }

x

int g(int x) { int y; y = h(x); return y; }

int h (int z) { return z + 1; }

x 3

y

z 3

int main(){ f(); return 0; }

f

g

h

CMSC 330 7

How About This?

– (Equivalent to...)

let addN (n, l) = let add x = n + x in map (add, l)

Accessing variable

from outer scope

let addN (n, l) = map ((fun x -> n + x), l)

takes a number n and list l and

adds n to every element in l

CMSC 330 8

Consider the Call Stack Again

• Uh oh...how does add know the value of n?

– The wrong answer for OCaml: it reads it off the stack

  • The language could do this, but can be confusing (see above)

– OCaml uses static scoping like C, C++, Java, and Ruby

let addN (n, l) =

map (add, l)

let map (f, n) = match n with [] -> [] | (h::t) -> (f h)::(map (f, t))

addN (3, [1; 2; 3])

let add x = n + x in

n 3

l

f

n

x 1

CMSC 330 9

Static Scoping

• In static or lexical scoping , (nonlocal) names

refer to their nearest binding in the program text

– Going from inner to outer scope

– C example:

– In our example, add accesses addN’s n

int x; void f() { x = 3; } void g() { char *x = "hello"; f(); }

Refers to the x at file scope – that’s

the nearest x going from inner scope

to outer scope in the source code

CMSC 330 10

Returned Functions

• As we saw, in OCaml a function can return

another function as a result

– So consider the following example

– When the anonymous function is called, n isn’t even

on the stack any more!

  • We need some way to keep n around after addN returns

let addN n = (fun x -> x + n) (addN 3) 4 (* returns 7 *)

CMSC 330 13

Another Example

let mult_sum (x, y) = let z = x + y in fun w -> w * z

(mult_sum (3, 4)) 5  5  5 * 7 (^)  35

CMSC 330 14

Yet Another Example

let twice (n, y) = let f x = x + n in f (f y)

twice (3, 4)  ( 4)  7  10

CMSC 330 15

Still Another Example

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

(((add 1) 2) 3)  (( 2) 3)  ( 3)  1+2+

CMSC 330 16

Currying

• We just saw another way for a function to take

multiple arguments

– The function consumes one argument at a time,

creating closures until all the arguments are available

• This is called currying the function

– Named after the logician Haskell B. Curry

– But Schƶnfinkel and Frege discovered it

  • So it should probably be called Schƶnfinkelizing or Fregging

CMSC 330 19

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))

CMSC 330 20

Currying and the map Function

• Examples

let negate x = -x

map negate [1; 2; 3] (* returns [-1; -2; -3 ] *)

let negate_list = map negate

negate_list [-1; -2; -3]

let sum_pairs_list = map (fun (a, b) -> a + b)

sum_pairs_list [(1, 2); (3, 4)] (* [3; 7] *)

• What's the type of this form of map?

let rec map f l = match l with [] -> [] | (h::t) -> (f h)::(map f t)

map : ('a -> 'b) -> 'a list -> 'b list

CMSC 330 21

Currying and the fold Function

let rec fold f a l = match l with [] -> a | (h::t) -> fold f (f a h) t

let add x y = x + y

fold add 0 [1; 2; 3]

let sum = fold add 0

sum [1; 2; 3]

let next n _ = n + 1

let length = fold next 0 (* warning: not polymorphic *)

length [4; 5; 6; 7]

• What's the type of this form of fold?

fold : ('a -> 'b -> 'a) -> 'a -> 'b list -> 'a

CMSC 330 22

Another Convention

• Since functions are curried, function can often

be used instead of match

– function declares an anonymous function of one

argument

– Instead of

– It could be written

let rec sum l = match l with [] -> 0 | (h::t) -> h + (sum t)

let rec sum = function [] -> 0 | (h::t) -> h + (sum t)

CMSC 330 25

Higher-Order Functions in C

• C has function pointers but no closures

– (gcc has closures)

typedef int (*int_func)(int);

void app(int_func f, int *a, int n) { int i; for (i = 0; i < n; i++) a[i] = f(a[i]); }

int add_one(int x) { return x + 1; }

int main() { int a[] = {1, 2, 3, 4}; app(add_one, a, 4); }

CMSC 330 26

Higher-Order Functions in Ruby

• Use yield within a method to call a code block

argument

def my_collect(a)

b = Array.new(a.length)

i = 0

while i < a.length

b[i] = yield(a[i])

i = i + 1

end

return b

end

b = my_collect([1, 2, 3, 4, 5]) { |x| -x }

CMSC 330 27

Higher-Order Functions in Java/C++

• An object in Java or C++ is kind of like a closure

– it’s some data (like an environment)

– along with some methods (i.e., function code)

• So objects can be used to simulate closures

• When we get to Java in the course, we’ll study

how to implement some functional patterns in

OO languages