IN ML: fun g x = let val inc = 1; fun f y = y + inc; fun h z = let val inc = 2;
ID: 3734854 • Letter: I
Question
IN ML:
fun g x =
let
val inc = 1;
fun f y = y + inc;
fun h z =
let
val inc = 2;
in
f z
end;
in
h x
end;
a) Draw a circle around every block, and number the blocks
b) Identify each definition of a name
c) For each definition, describe its scope in terms of your block numbers
d) For each occurrence of a name (other than a definition of that name), show which definition is used to bind it. Check that this agrees with your scopes
Explanation / Answer
Solution:
The blocks are given in terms of the code snippet, if you face any issue to understand, let me know in the comments.
fun g x =
let
val inc = 1;
fun f y = y + inc;
fun h z =
let
val inc = 2;
in
f z
end;
in
h x
end;
Let's call this section as block 1.
et
val inc = 1;
fun f y = y + inc;
fun h z =
let
val inc = 2;
in
f z
end;
in
Let's call this section as block 2
fun h z =
let
val inc = 2;
in
f z
end;
this is block 3
let
val inc = 2;
in
block 4
b)
g x and h z are the definitions of a name
c)
for g x the scope is in block 1, but not in block 3
for h x the scope is in block 3.
d)
for g x
let
val inc = 1;
fun f y = y + inc;
fun h z =
let
val inc = 2;
in
f z
end;
in
is used to bind it.
for h z
let
val inc = 2;
in
is used to bind
I hope this helps if you find any problem. Please comment below. Don't forget to give a thumbs up if you liked it. :)