

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
CMSC 330 3
CMSC 330 4
CMSC 330 5
(* return a list containing all the elements in the list l followed by all the elements in list m *)
CMSC 330 6
let rec rev_helper (l, a) = match l with [] -> a | (x::xs) -> rev_helper (xs, (x::a)) let rev l = rev_helper (l, [])
CMSC 330 7
CMSC 330 8
a = [1,2,3,4,5] b = a.collect { |x| -x }
CMSC 330 9
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 10
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))
map : ('a -> 'b) * 'a list -> 'b list
CMSC 330 11
map ((fun x -> x + 13), [1; 2; 3]) twice ((fun x -> x + 2), 4)
fun x -> x + 3
CMSC 330 12
map ((fun l -> match l with (h::_) -> h), [ [1; 2; 3]; [4; 5; 6; 7]; [8; 9] ]) (* [1; 4; 8] *)
map ((fun (x, y) -> x + y), [(1, 2); (3, 4)]) (* [3; 7] *)