

Prepara tus exámenes y mejora tus resultados gracias a la gran cantidad de recursos disponibles en Docsity
Gana puntos ayudando a otros estudiantes o consíguelos activando un Plan Premium
Esta página no es visible en la vista previa
¡No te pierdas las partes importantes!


(* Type Inference ) ( simulate type checker (in a way) ) ( this is given: abs: int -> int ) fun f x = let val (y,z) = x in (abs y) + z end ( f: T1 -> T2 x: T y: T z: T T1 = T3T T3 = int T4 = int T2 = int f: intint -> int x: int ) fun sum xs = case xs of [] => 0 | x::xs' => x + (sum xs') ( sum: T1 -> T2 xs: T T1 = T3 list T2 = int xs': T1 = T3 list x: T T3 = T2 = int sum: int list -> T2 ) fun broken_sum xs = case xs of [] => 0 | x::xs' => x + (broken_sum x) ( broken_sum: T1 -> T2 xs: T T1 = T3 list T2 = int xs': T3 list x: T T3 = T2 = int T3 = T3 list ) fun length xs = case xs of ] => 0 | x::xs' => 1 + (length xs') ( length: T1 -> T2 xs: T T1 = T3 list T2 = int xs': T3 list (=T1) x: T length: 'a list -> int xs: 'a list ) fun f (x,y,z) = if true then (x,y,z) else (y,x,z) ( f: T1 * T2 * T3 -> T4 x: T1, y: T2, z: T3, T1 = T T4 = T1T1T f: 'a * 'a * 'b -> 'a * 'a * 'b T1T1T3 -> T1T2T ) fun compose (f,g) = fn x => f (g x) ( compose: T1 * T2 -> T3 f: T g: T anon func: T3 = T4 -> T x: T g: T1= T4-> T6 f: T2= T6-> T compose: T6->T5 * T4->T6 -> T4->T5 'a->'b * 'c->'a -> 'c->'b ) signature RATIONAL_A = sig datatype rational = Frac of int * int | Whole of int exception BadFrac val make_frac : int * int -> rational val add : rational * rational -> rational val toString : rational -> string end structure Rational1 :> RATIONAL_A = struct ( Invariant 1: all denominators > 0 Invariant 2: rationals kept in reduced form ) datatype rational = Whole of int | Frac of intint exception BadFrac (* gcd and reduce help keep fractions reduced, but clients need not know about them ) ( they assume their inputs are not negative ) fun gcd (x,y) = if x=y then x else if x < y then gcd(x,y-x) else gcd(y,x) ( Frac(5, 10) ==> Frac(1, 2) Frac(10, 5) ==> Whole(2) ) fun reduce r = case r of Whole _ => r | Frac(x,y) => if x= then Whole(0) else let val d = gcd(abs x, y) ( using invariant 1 ) in if y=d then Whole(x div d) else Frac(x div d, y div d) end ( when making a frac, we ban zero denominators ) fun make_frac (x,y) = ( enforce invariant 1 (denom > 0) ) ( must handle div by zero ) if y= then raise BadFrac else if y < 0 then reduce Frac(~x, ~y) else reduce Frac(x, y) ( using math properties, both invariants hold of the result assuming they hold of the arguments ) fun add (r1,r2) = case (r1,r2) of (Whole(i),Whole(j)) => Whole(i+j) | (Whole(i),Frac(j,k)) => (i+j/k ==> (ik+j)/k ) Frac(ik+j, k) | (Frac(j,k),Whole(i)) => Frac(ik+j, k) | (Frac(a,b),Frac(c,d)) => (* a/b+c/d = (ad+cb)/bd ) reduce Frac(ad+cb, bd) (* given invariant, prints in reduced form ) fun toString r = case r of Whole i => Int.toString i ( 1=>"1", 2=>"2" ) | Frac(a,b) => (Int.toString a) ^ "/" ^ (Int.toString b) (1/2 ==> "1/2" ) end fun count_multiples (x, nums_list) = let val multiples = filter(fn num => num mod x = 0, nums_list) in map(List.length, multiples) end ( similar to above, given x, count the multiples of x in each list * and returns the index of the list having the maximum count. * x=11, [[1, 2, 11], [11, 22, 33], [4, 5]] *==> 1 ) ( 1. call count_multiples above
fun index_of_max_multiple_count (x, nums_list) = let val counts = (* [1, 3, 3] ) in fold( (0, 0, hd(counts)), …………..counts) end ( abstract data type using closures ) ( int set
g_val = 42; def myadd(val, g_val = g_val): return val+g_val f = myadd g_val = 100 result = map(f, [1,2,3,4]) print(list(result)) datatype exp = Constant of int | Negate of exp | Add of exp * exp | Mul val myexp1 = Add(Constant(42), Multiply(Constant(40), Negate(Constant(10)))); (* 42 + (40 * (- 10)) )tiply of exp * exp val myexp2 = Add(Constant(41), Multiply(Constant(40), Negate(Constant(10)))); ( 41 + (40 * (- 10)) ) ( exp -> bool, true ) fun all_even (e) = case e of Constant i | Negate e | Add(e1,e2) | Multiply(e1,e2) => all_even(e1) andalso all_even(e2) fun all_odd (e) = case e of Constant i | Negate e | Add(e1,e2) | Multiply(e1,e2) => all_odd(e1) andalso all_odd(e2) fun all (test: int->bool, e) = case e of => (i mod 2) = 0 => all_even(e2) => all_even(e1) andalso all_even(e2) => (i mod 2) = 1 => all_odd(e2) => all_odd(e1) andalso all_odd(e2) Constant i | Negate e | Add(e1,e2) | Multiply(e1,e2) => all(test,e1) andalso all(test, e2) ( another version of all_even ) fun all_even(e) = all((fn x=> (x mod 2) = 0), e) fun all_odd(e) = all((fn x=> (x mod 2) = 1), e) fun any (test, e) = => test(i) => all(test, e2) => all(test,e1) andalso all(test, e2) case e of Constant i | Negate e | Add(e1,e2) | Multiply(e1,e2) => any(test, e1) orelse any(test, e2) fun any_even(e) = any((fn x => (x mod 2) = 0), e) fun any_odd(e) =any((fn x=> (x mod 2) = 1), e) fun add_or_mult (f, v) = if f 7 then fn x => vx else fn x => v+x val func1 = add_or_mult((fn x => (x mod 7)=0), 42) func1(100) 2222... v fun double_n_times(n, v) = if n= then v else double_n_times(n-1, v2) v+1+1+1+1... fun increment_n_times(n, v) = if n= then v else increment_n_times(n-1, v+1) fun nth_elm(n, lst) = if n= then hd(lst) => test(i) => any(test, e2) => any(test, e1) orelse any(test, e2) else nth_elm(n-1, tl lst) (