Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. What is the value of ans after the following ML code is executed? fun silly2

ID: 3590052 • Letter: 1

Question

1. What is the value of ans after the following ML code is executed?      

   fun silly2 () =

       let val x = 1

       in

       (let val x = 2 in x+1 end) + (let val y = x+2 in y+1 end)

   end

       val ans = silly2()

  

2. What value is bound to ans after the following ML code is evaluated?  

   fun mystery(x:int option) =

       if isSome x

       then let val y = (valOf x) in y * y end

   else 0

       val ans = mystery(SOME 3)

3. Given this ML list: val e = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

   What is the value of: hd e  

4. Which tuples have the type

   int * bool * int      

   a. (5, false, 8)

   b. (5, false)

   c. (7, true, 9, 5)

   d. (false, 8)

   e. (9, 5)

   f. (true, 5)

   g. (7, (true, 9, 5))

   h. (7, true)

   i. (3, 4)

   j. (8, 3, 4)

5. Given this ML list:

   val e = [[0, 1], [2, 3], [4, 5], [6, 7], [8, 9]]      
   What is the value of:

       hd (hd (tl (tl e)))

6. What does silly3 evaluate to when called?

   fun silly3() =      

       let

           val x = (let val x = 4 in x + 8 end);

        in

           (x, let val x = 7 in x end, let val x = 2 in let val x = 4 in x end end)

       end

   a. (12, 7, 2)

   b. (4, 4, 4)

   c. (5, 7, 2)

   d. (12, 7, 4)

7. A list would be more appropriate than a tuple for storing the name of each student who registers for a class.

   a. True

   b. False

8. What does silly3 evaluate to when called?

   fun silly3() =      

       let

           val x = (let val x = 2 in x + 4 end);

       in

           (x, let val x = 3 in x end, let val x = 7 in let val x = 1 in x end end)

       end

Explanation / Answer

1.Ouptut : 6

ans calls silly2() where x is assigned 1 and then

(let val x = 2 in x+1 end) ---- val x = (2 in 2 )

So ,x = 3

+ (let val y = x+2 in y+1 end) --- 3 in 2

So, here output 4

So x =x in (3 + 4)

output: 6

2.OUTPUT = 9

mystery(SOME 3) is called

so (x:int option) get 3 as parameter

val y = (valOf x) in y * y end

y = (x) in 3*3

output = 9

3. OUTPUT:0

val e = hd[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

d list(here e) returns first element.

4.

a. (5, false, 8) -- yes same as required type

   b. (5, false) -- no only 2 parameters

   c. (7, true, 9, 5) -- no more parameters

   d. (false, 8) -- no less number of parameters

   e. (9, 5) -- no less number of parameters

   f. (true, 5) --  no less number of parameters

   g. (7, (true, 9, 5)) -- no less number of parameters if you clearly see parenthesis

   h. (7, true) -- no less number of parameters

   i. (3, 4) -- no less number of parameters

   j. (8, 3, 4)