3rd Sem PYQ Solved
3rd Sem PYQ Solved
let mul c1 c2 = {real = ((c1.real * c2.real) – (c1.img * c2.img)); img = ((c1.real * c2.img) +
(c2.real * c1.img))}
end
---------------------------------------------------------------------------
2.2
The first one [ref 5; ref 5] is a list of 2 different references, and the second one is a list of 2
same references let x = ref 5 in [x;x]
-------------------------------------------------------------------------
3.1
A functor is a higher order module that takes one or more modules as input and returns a
new module as output. It is important in that it allows for code reusability; it abstracts away
implementation details and allows generalized designs.
-------------------------------------------------------------------------
3.2
Yes. The ‘a ref types are considered mutable in that they allow for the value stored in
memory to be mutated. While the ref variables themselves are not mutable and will always
hold the same address location, the value stored in that address is mutable.
-----------------------------------------------------------------------
4.1
---------------------------------------------------------------------
4.2
Let rec from n = Cons((if(n mod 3 = 0) then ‘a’ else if (n mod 3) then ‘b’ else ‘c’), fun()->
from(n+1))
----------------------------------------------------------------
5.1
1;
2;
let hd (Cons(h,t)) = h
let tl (Cons(h,t)) = t ()
let rec map seq f = Cons((f (hd seq)), fun () -> map (tl seq) f)
3;
---------------------------------------------------------------
6.1
1;
2;
3;
---------------------------------------------------------------
7.1
1;
let hd (Cons(h,t)) = h
let tl (Cons(h,t)) = t ()
let rec filter seq p = if (p (hd seq)) then Cons((hd seq), fun()-> filter (tl seq) p) else filter (tl
seq) p
2;
----------------------------------------------------------
7.2
1;
end
2;
type ‘a t = ‘a option