ML Self Test - Tutorial Two

[ First Question ] Number of questions : 3

Welcome to the multiple choice question session.

This material is based on Tutorial Two of the ML Manual.Nobody is watching or keeping score so feel free to guess or to explore those answers which you suspect or know are wrong.

Some of the questions will not be immediately obvious from the tutorial material alone.































ML Self Test - Tutorial Two

[ Previous Question | Next Question ]

1. Select the expression which will evaluate to [1,2,3,4]

a) 1::[2,3]@[4]
b) [1]::[2,3]@[4]
c) explode "1234"
d) [1,2,3]::4
e) 1::2::3::4

































ML Self Test - Tutorial Two

[ Previous Question | Next Question ]

2. Select the result of the binding attempt:

val (_,b::_) = ([1,2,3],[4,5,6]);
a) val b = 4 : int list
b) val b = [5,6] : int list
c) val b = 1
d) tychon mismatch
e) val b = 4 : int

































ML Self Test - Tutorial Two

[ Previous Question | Next Question ]

3. Select the expression which breaks ML's type rules:

a) ([3],["three"])
b) [[(),()]]
c) [([],[],[1]),([],["one"],[])]
d) [(3),("three")]
e) [ord, size]

































































ML Self Test - Tutorial Two

[ Return to Question | Next Question ]

1.Select the expression which will evaluate to [1,2,3,4]
a) 1::[2,3]@[4]

Well done.

It does not matter whether the :: or @ is evaluated first, the result will be [1,2,3,4].
































ML Self Test - Tutorial Two

[ Return to Question ]

1.Select the expression which will evaluate to [1,2,3,4]
b) [1]::[2,3]@[4]

This expression is not well formed.

The :: operator requires an int on the left hand side - here it has an int list.
































ML Self Test - Tutorial Two

[ Return to Question ]

1.Select the expression which will evaluate to [1,2,3,4]
c) explode "1234"

Not quite.

This gives the list ["1","2","3","4"] which is a string list rather than the int list required.
































ML Self Test - Tutorial Two

[ Return to Question ]

1.Select the expression which will evaluate to [1,2,3,4]
d) [1,2,3]::4

No.

The :: operator wants an int on the left and a list on the right, here it is a list on the left and an int on the right.
































ML Self Test - Tutorial Two

[ Return to Question ]

1.Select the expression which will evaluate to [1,2,3,4]
e) 1::2::3::4

This is a common error.

The last :: operator requires a list on the right but here it has the integer 4. A sequence of :: operators must always end in a list, often the list is nil, for example 1::2::3::4::nil would be correct.
































ML Self Test - Tutorial Two

[ Return to Question ]

2.Select the result of the binding attempt:
val (_,b::_) = ([1,2,3],[4,5,6]);
a) val b = 4 : int list

No that is not right.

4 is of type int not int list.
































ML Self Test - Tutorial Two

[ Return to Question ]

2.Select the result of the binding attempt:
val (_,b::_) = ([1,2,3],[4,5,6]);
b) val b = [5,6] : int list

This is incorrect.

b will be bound to the head of the second element of the tuple.
































ML Self Test - Tutorial Two

[ Return to Question ]

2.Select the result of the binding attempt:
val (_,b::_) = ([1,2,3],[4,5,6]);
c) val b = 1

No.

The expression b::_ must match the second element of the tuple namely the list [4,5,6]
































ML Self Test - Tutorial Two

[ Return to Question ]

2.Select the result of the binding attempt:
val (_,b::_) = ([1,2,3],[4,5,6]);
d) tychon mismatch

Those pesky tychons.

This time the tychons are matched to perfection, even if the binding is somewhat less than exhaustive.
































ML Self Test - Tutorial Two

[ Return to Question | Next Question ]

2.Select the result of the binding attempt:
val (_,b::_) = ([1,2,3],[4,5,6]);
e) val b = 4 : int

Got it!

The right hand side of the equation is the tuple ([1,2,3],[4,5,6]).
The first component of the tuple: [1,2,3] matches the first _ which is effectively "thrown away", the second component: [4,5,6] is matched to the expression b::_ This means that b gets bound to the head of [4,5,6] while the tail of [4,5,6] gets ditched.
































ML Self Test - Tutorial Two

[ Return to Question ]

3.Select the expression which breaks ML's type rules:
a) ([3],["three"])

This object is fine.

This expression is valid, it has type int list * string list . It is a two tuple, the first component is an int list, the second is a string list.
































ML Self Test - Tutorial Two

[ Return to Question ]

3.Select the expression which breaks ML's type rules:
b) [[(),()]]

Nothing wrong with this expression.

This peculiar object is OK. It has type unit list list . The () object is the only instance of a type called unit. This is a list of a list of a pair of them.
































ML Self Test - Tutorial Two

[ Return to Question ]

3.Select the expression which breaks ML's type rules:
c) [([],[],[1]),([],["one"],[])]

This one is OK.

It turns out that this object is of type ('a list * string list * int list) list. The list contains two tuples, which must agree in type, the first tuple ([],[],[1]) requires the third component to be an integer, the second tuple ([],["one",],[]) requires the second component to be a string. Neither places any restrictions on the type of the first list.
































ML Self Test - Tutorial Two

[ Return to Question | Next Question ]

3.Select the expression which breaks ML's type rules:
d) [(3),("three")]

This expression is not well formed.

All the components of a list must agree in type. These do not.
































ML Self Test - Tutorial Two

[ Return to Question ]

3.Select the expression which breaks ML's type rules:
e) [ord, size]

Even this expression is acceptable.

ML has no problem treating unevaluated functions as objects, be they members of a list or a tuple. This has type (string -> int) list
































ML Self Test - Tutorial Two

[ Previous Question | Back home ]

That is all for now.