Week 5 Quiz Answers
Week 5 Quiz Answers
in the environment
correct
ML is statically typed
correct
Question 2
CODE
For each pattern below, check the box if and only if this pattern
matches the value above.
x::y
correct
x::(y::z)
correct
(a,b,c)::d
[]
(a,b)::(c,d)::(e,f)::[]
correct
(a,b)::(c,d)::(e,f)::g
correct
Question 3
For each of the statements below, check the box if and only if the
statement is true regarding this ML code:
CODE
1 fun mystery f xs =
2 let
3 fun g xs =
4 case xs of
5 [] => NONE
6 | x::xs' => if f x then SOME x else g
7 in
8 case xs of
9 [] => NONE
correct
correct
correct
The result type of f can be any type, but it must be the same
If you replace the first line of the code with fun mystery f =
type-check.
If you replace the first line of the code with fun mystery f =
result.
g is a tail-recursive function.
correct
and someList ).
correct
and someList ).
Test code:
CODE
1 fun mystery f xs =
2 let
3 fun g xs =
4 case xs of
5 [] => NONE
6 | x::xs' => if f x then SOME x else g
7
7
in
8
case xs of
9
result:
CODE
Question 4
null below, check the box if and only if the function would behave
the same as the predefined null function whenever the function below
is called.
Note: Consider only situations where calls to the functions below type-
check.
correct
correct
correct
Question 5
The next four questions, including this one, relate to this situation:
strings, but the details do not matter). The library includes higher-
order functions map, filter, and fold that operate on these collections
and have their conventional meanings. For each problem below,
(For those needing a precise definition of best: On this exam, the best
used by passing it the function argument that itself does the least
amount of work.)
collection where each string in the output is like a string in the input
map
correct
filter
fold
Question 6
map
filter
fold
correct
Question 7
Desired function: Take a collection of strings and a number n and
multiple of n.
map
filter
fold
correct
Question 8
containing the strings in the input collection that start with a capital
letter.
map
filter
correct
fold
Question 9
This datatype binding and type synonym are useful for representing
CODE
(x + 4) + z = 7 ⋅ y(x + 4) + z = 7 ⋅ y
3 2 0
x .y = z
14.2 + 3 = 17.2
y
x = z
correct
Question 10
CODE
For each type below, check the box if and only if the type is an
instantiation of the type above, which means the type above is more
general.
correct
correct
c : int}
correct
Question 11
The next 5 questions, including this one, are similar. Each question
CODE
to use the structure in different ways. You will answer the same
CODE
1 signature COUNTER =
2 sig
3 type t = int
4 val newCounter : int -> t
5 val increment : t -> t
6 val first_larger : t * t -> bool
7 end
raised.
NoNegativeCounter.first_larger to be evaluated).
This signature makes it possible for clients to call
Question 12
CODE
1 signature COUNTER =
2 sig
3 type t = int
4 val newCounter : int -> t
5 val first_larger : t * t -> bool
6 end
NoNegativeCounter.InvariantViolated exception to be
raised.
NoNegativeCounter.first_larger to be evaluated).
Question 13
CODE
1 signature COUNTER =
2 sig
3 type t
4 val newCounter : int -> int
5 val increment : t -> t
6 val first_larger : t * t -> bool
7 end
NoNegativeCounter.InvariantViolated exception to be
raised.
NoNegativeCounter.first_larger to be evaluated).
instantiated
Question 14
CODE
1 signature COUNTER =
2 sig
3 type t
4 val newCounter : int -> t
5 val increment : t -> t
6 val first_larger : t * t -> bool
7 end
NoNegativeCounter.InvariantViolated exception to be
raised.
This signature makes it impossible for any client to call
NoNegativeCounter.first_larger to be evaluated).
Question 15
CODE
1 signature COUNTER =
2 sig
3 type t = int
4 val newCounter : int -> t
5 val increment : t -> t
6 end
NoNegativeCounter.InvariantViolated exception to be
raised.
NoNegativeCounter.first_larger to be evaluated).