



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




CMSC 330 2
let rec print_up_to (n, m) = print_int n; print_string "\n"; if n < m then print_up_to (n + 1, m)
CMSC 330 3
let rec length l = match l with [] -> 0 | (_::t) -> 1 + (length t)
CMSC 330 4
sum l (* sum of elts in l *) let rec sum l = match l with [] -> 0 | (x::xs) -> x + (sum xs)
negate l (* negate elements in list *) let rec negate l = match l with [] -> [] | (x::xs) -> (-x) :: (negate xs)
CMSC 330 5
last l (* last element of l *) let rec last l = match l with [x] -> x | (_::xs) -> last xs
append (l, m) (* list containing all elements in list l followed by all elements in list m *) let rec append (l, m) = match l with [] -> m | (x::xs) -> x::(append (xs, m))
CMSC 330 6
rev l (* reverse list; hint: use append *) let rec rev l = match l with [] -> [] | (x::xs) -> append ((rev xs), [x])
CMSC 330 7
let rec rev_helper (l, a) = match l with [] -> a | (x::xs) -> rev_helper (xs, (x::a)) let rev l = rev_helper (l, [])
rev [1; 2; 3] → rev_helper ([1;2;3], []) → rev_helper ([2;3], [1]) → rev_helper ([3], [2;1]) → rev_helper ([], [3;2;1]) → [3;2;1]
CMSC 330 8
flattenPairs l (* ('a * 'a) list -> 'a list *) let rec flattenPairs l = match l with [] -> [] | ((a, b)::t) -> a :: b :: (flattenPairs t)
take (n, l) (* return first n elements of l *) let rec take (n, l) = if n = 0 then [] else match l with [] -> [] | (x::xs) -> x :: (take (n-1, xs))
CMSC 330 9
a = [1,2,3,4,5] b = a.collect { |x| -x }
CMSC 330 10
let plus_three x = x + 3 let twice (f, z) = f (f z) twice (plus_three, 5) twice : ('a->'a) * 'a -> 'a
let plus_four x = x + 4 let pick_fn n = if n > 0 then plus_three else plus_four (pick_fn 5) 0 pick_fn : int -> (int->int)
CMSC 330 11
let add_one x = x + 1 let negate x = -x map (add_one, [1; 2; 3]) map (negate, [9; -5; 0])
let rec map (f, l) = match l with [] -> [] | (h::t) -> (f h)::(map (f, t))
CMSC 330 12
('a -> 'b) * 'a list -> 'b list
f l
let rec map (f, l) = match l with [] -> [] | (h::t) -> (f h)::(map (f, t))
CMSC 330 19
let add (a, x) = a + x fold (add, 0, [1; 2; 3; 4]) → fold (add, 1, [2; 3; 4]) → fold (add, 3, [3; 4]) → fold (add, 6, [4]) → fold (add, 10, []) → 10
We just built the sum function!
let rec fold (f, a, l) = match l with [] -> a | (h::t) -> fold (f, f (a, h), t)
CMSC 330 20
let next (a, _) = a + 1 fold (next, 0, [2; 3; 4; 5]) → fold (next, 1, [3; 4; 5]) → fold (next, 2, [4; 5]) → fold (next, 3, [5]) → fold (next, 4, []) → 4
We just built the length function!
let rec fold (f, a, l) = match l with [] -> a | (h::t) -> fold (f, f (a, h), t)
CMSC 330 21
let prepend (a, x) = x::a fold (prepend, [], [1; 2; 3; 4]) → fold (prepend, [1], [2; 3; 4]) → fold (prepend, [2; 1], [3; 4]) → fold (prepend, [3; 2; 1], [4]) → fold (prepend, [4; 3; 2; 1], []) → [4; 3; 2; 1]
let rec fold (f, a, l) = match l with [] -> a | (h::t) -> fold (f, f (a, h), t)
CMSC 330 22
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
int main(){ f(); return 0; }
f g
h
CMSC 330 23
let pick_one n = if n > 0 then (fun x -> x + 1) else (fun x -> x - 1) (pick_one -5) 6 (* returns 5 *)
let sum l = fold ((fun (a, x) -> a + x), 0, l)
CMSC 330 24
let sum l = let add (a, x) = a + x in fold (add, 0, l)
let pick_one n = let add_one x = x + 1 in let sub_one x = x - 1 in if n > 0 then add_one else sub_one
CMSC 330 25
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)
CMSC 330 26
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
CMSC 330 27
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