ML Self Test - Tutorial One

[ First Question ] Number of questions : 4

Welcome to the multiple choice question session.

This material is based on Tutorial One 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 One

[ Previous Question | Next Question ]

1. Select the syntactically correct function definition:

a) fun switch s = substring(s,4,size - 4) ^ substring(s,0,4)
b) fun shift s = substring(s, 2, size s);
c) fun addVAT x = x*(100 + 17.5)/100;
d) fun hexadecaple x = quadruple quadruple x;
e) fun (x,y) = x+y;

































ML Self Test - Tutorial One

[ Previous Question | Next Question ]

2. Given the following function definitions:

fun double x = 2 * x;
fun triple x = 3 * x;
What is the result of double(triple(size "seven"));
a) tychon mismatch
b) "fourty two"
c) 30 : int
d) 25 : int
e) 42 : int

































ML Self Test - Tutorial One

[ Previous Question | Next Question ]

3. The function sndhalf takes a string and returns the second half of it. We do not care what happens for a string of odd length. Select the correct function definition.

a) fun sndhalf s = substring(s,size div 2,size div 2);
b) fun sndhalf s = substring(s, size s / 2, size s / 2);
c) fun sndhalf s = substring(s, size s div 2, size s div 2);
d) fun sndhalf s = substring(s, size s div 2 + 1, size s);
e) fun sndhalf s = substring(s,(size s - 1) div 2, size s);

































ML Self Test - Tutorial One

[ Previous Question | Next Question ]

4. Given the following function definition:

fun f s = substring(s,1,size s - 1) ^ substring(s,0,1);
The result of the call f(f("elbow")) is:
a) =
b) f(substring("elbow",1, 4) ^ substring("elbow",0,1))
c) "bowel"
d) "elbow"
e) ... tychon mismatch

































































ML Self Test - Tutorial One

[ Return to Question ]

1.Select the syntactically correct function definition:
a) fun switch s = substring(s,4,size - 4) ^ substring(s,0,4)

This will not work.

The right hand side of the function definition is invalid. The type checker will fail as the function size is used without a string input.
































ML Self Test - Tutorial One

[ Return to Question | Next Question ]

1.Select the syntactically correct function definition:
b) fun shift s = substring(s, 2, size s);

Correct.

But you will not be able to evaluate this function for any string. The function will attempt to extract size s characters from position 2 and will always give an error. You must be careful when using partial functions such as substring.
































ML Self Test - Tutorial One

[ Return to Question ]

1.Select the syntactically correct function definition:
c) fun addVAT x = x*(100 + 17.5)/100;

This will not work.

ML is irritatingly fussy about mixing reals and integers. You can convert between real and integer numbers using the functions real and floor.
































ML Self Test - Tutorial One

[ Return to Question ]

1.Select the syntactically correct function definition:
d) fun hexadecaple x = quadruple quadruple x;

This fails the type checker.

The problem with this definition is that ML attempts to apply the function quadricate to itself rather than applying quadricate to x then quadricate to the result. By default the brackets go:(quadricate quadricate)x rather than quadricate(quadricate x) as intended.
The result is a tycon mismatch which is a very bad thing to happen. If your tycons don't match then people will laugh at you behind your back.
































ML Self Test - Tutorial One

[ Return to Question ]

1.Select the syntactically correct function definition:
e) fun (x,y) = x+y;

This is not a proper function definition.

The function name has been ommitted. It is possible to define functions without naming them - however this is not how you do it.
































ML Self Test - Tutorial One

[ Return to Question ]

2.Given the following function definitions:
fun double x = 2 * x;
fun triple x = 3 * x;
What is the result of double(triple(size "seven"));
a) tychon mismatch

There is no type error here.

The expression is well defined, size returns an integer to triple which returns another integer to double.
































ML Self Test - Tutorial One

[ Return to Question ]

2.Given the following function definitions:
fun double x = 2 * x;
fun triple x = 3 * x;
What is the result of double(triple(size "seven"));
b) "fourty two"

No.

That is the answer to a different to question.
































ML Self Test - Tutorial One

[ Return to Question | Next Question ]

2.Given the following function definitions:
fun double x = 2 * x;
fun triple x = 3 * x;
What is the result of double(triple(size "seven"));
c) 30 : int

Well done.

size return 5 to triple which returns 15 to double which returns 30
































ML Self Test - Tutorial One

[ Return to Question ]

2.Given the following function definitions:
fun double x = 2 * x;
fun triple x = 3 * x;
What is the result of double(triple(size "seven"));
d) 25 : int

No.

Try another answer.
































ML Self Test - Tutorial One

[ Return to Question ]

2.Given the following function definitions:
fun double x = 2 * x;
fun triple x = 3 * x;
What is the result of double(triple(size "seven"));
e) 42 : int

No.

Try a smaller value.
































ML Self Test - Tutorial One

[ Return to Question ]

3.The function sndhalf takes a string and returns the second half of it. We do not care what happens for a string of odd length. Select the correct function definition.
a) fun sndhalf s = substring(s,size div 2,size div 2);

This is wrong.

The function size is used, but is not given an input.
































ML Self Test - Tutorial One

[ Return to Question ]

3.The function sndhalf takes a string and returns the second half of it. We do not care what happens for a string of odd length. Select the correct function definition.
b) fun sndhalf s = substring(s, size s / 2, size s / 2);

This gives a type error.

The function size returns an integer, but / only works with reals. The function div should be used instead.
































ML Self Test - Tutorial One

[ Return to Question | Next Question ]

3.The function sndhalf takes a string and returns the second half of it. We do not care what happens for a string of odd length. Select the correct function definition.
c) fun sndhalf s = substring(s, size s div 2, size s div 2);

That is correct.


































ML Self Test - Tutorial One

[ Return to Question ]

3.The function sndhalf takes a string and returns the second half of it. We do not care what happens for a string of odd length. Select the correct function definition.
d) fun sndhalf s = substring(s, size s div 2 + 1, size s);

This is not quite right.

The function substring takes the string and two numbers, the first number is the start position (counting the first character as 0), the second parameter is the size of the required substring.
































ML Self Test - Tutorial One

[ Return to Question ]

3.The function sndhalf takes a string and returns the second half of it. We do not care what happens for a string of odd length. Select the correct function definition.
e) fun sndhalf s = substring(s,(size s - 1) div 2, size s);

That is not quite it.

The final parameter to substring is the number of characters in the returned string, not the end position of the substring within the input string.
































ML Self Test - Tutorial One

[ Return to Question ]

4.Given the following function definition:
fun f s = substring(s,1,size s - 1) ^ substring(s,0,1);
The result of the call f(f("elbow")) is:
a) =

No.

If you enter exactly the given expression without the semi colon then ML will indeed respond with =. However you should be evaluating the expressions yourself, using the machine to do it is cheating.
































ML Self Test - Tutorial One

[ Return to Question ]

4.Given the following function definition:
fun f s = substring(s,1,size s - 1) ^ substring(s,0,1);
The result of the call f(f("elbow")) is:
b) f(substring("elbow",1, 4) ^ substring("elbow",0,1))

Yes, but this can be further evaluated.

Each of the substring expressions must be evaluated (to "lbow" and "e" respectively), we must still apply f again.
































ML Self Test - Tutorial One

[ Return to Question | Next Question ]

4.Given the following function definition:
fun f s = substring(s,1,size s - 1) ^ substring(s,0,1);
The result of the call f(f("elbow")) is:
c) "bowel"

That's right.

It is important that ML users know their f(f("elbow"))s from their "elbow"s.
































ML Self Test - Tutorial One

[ Return to Question ]

4.Given the following function definition:
fun f s = substring(s,1,size s - 1) ^ substring(s,0,1);
The result of the call f(f("elbow")) is:
d) "elbow"

That is not right.

The functions f constructs a string made up of the first character concattenated onto the end of all but the first character. In the first application this means that the string "lbow" will have "e" appended. The function f is applied twice
































ML Self Test - Tutorial One

[ Return to Question ]

4.Given the following function definition:
fun f s = substring(s,1,size s - 1) ^ substring(s,0,1);
The result of the call f(f("elbow")) is:
e) ... tychon mismatch

No mismatch here.

The tychons here match nicely.
































ML Self Test - Tutorial One

[ Previous Question | Back home ]

That is all for now.