Standard ML

236319 Spring 2024, Prof. Lorenz


Standard ML

exam questions


question 1

winter 2020

q1_1_a

q1_1_b

...
fun foldlUntil f pred init [] = init
  | foldlUntil f pred init (x::xs) =
    let val next = f (x, init) in
      if pred next then next else foldlUntil f pred next xs
    end;

q1_2

...
local
  fun sublist s e l = if s > e orelse s > List.length(l) then [] else
      List.take(List.drop(l, s), e - s + 1);
in
  fun foldFromTo f s e init l = foldl f init (sublist s e l)
end;

q1_3

...
fun optionalFoldl fTrue fFalse pred init l =
    foldl (fn (a, b) => if pred a then fTrue (a,b) else fFalse (a,b)) init l;

question 3

winter 2020

q2

q2_1

Output: 12345678910
End value: 11

q2_2

Output: 1111111111
End value: 2

question 1

summer 2013 moed b

q3_1

exception e;
fun f x y z = if x(e) then raise z(x) else raise e;

q3_2

exception E1;
exception E2;
fun f(1) = raise E2 |
	f(2) = raise E1 |
	f(n) = ( let val x = f(n-2) handle E1 =>1 val y =
f(n-1) handle E2 => 2 in y + x end ) handle E1 => 3;
f(5);

q3_3

fun g (f,x) y = fn (z,x) => if (y > x) then z x else f z;

q3_4

...
fun unitList 0 = []
  | unitList n = ()::(unitList (n - 1));

fun divLists (_, []) = ([], [])
  | divLists (x, y) =
    let val (ix, iy) = (List.length x, List.length y) in
      (unitList (ix div iy), unitList (ix mod iy))
    end;